]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
a0b2ad65a0f68b61006b5078fe586a71880c13e0
[lyx.git] / src / mathed / math_nestinset.C
1 /**
2  * \file math_nestinset.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "math_nestinset.h"
14
15 #include "math_arrayinset.h"
16 #include "math_braceinset.h"
17 #include "math_commentinset.h"
18 #include "math_data.h"
19 #include "math_deliminset.h"
20 #include "math_factory.h"
21 #include "math_hullinset.h"
22 #include "math_mathmlstream.h"
23 #include "math_parser.h"
24 #include "math_scriptinset.h"
25 #include "math_spaceinset.h"
26 #include "math_support.h"
27 #include "math_mboxinset.h"
28 #include "math_unknowninset.h"
29
30 #include "BufferView.h"
31 #include "bufferview_funcs.h"
32 #include "cursor.h"
33 #include "debug.h"
34 #include "dispatchresult.h"
35 #include "funcrequest.h"
36 #include "gettext.h"
37 #include "LColor.h"
38 #include "undo.h"
39
40 #include "support/std_sstream.h"
41 #include "support/lstrings.h"
42
43 #include "frontends/Dialogs.h"
44 #include "frontends/LyXView.h"
45 #include "frontends/Painter.h"
46
47
48 using std::endl;
49 using std::string;
50 using std::istringstream;
51
52
53 namespace {
54
55 // local global
56 int first_x;
57 int first_y;
58
59 } // namespace anon
60
61
62
63
64 MathNestInset::MathNestInset(idx_type nargs)
65         : cells_(nargs), lock_(false)
66 {}
67
68
69 MathInset::idx_type MathNestInset::nargs() const
70 {
71         return cells_.size();
72 }
73
74
75 MathArray & MathNestInset::cell(idx_type i)
76 {
77         return cells_[i];
78 }
79
80
81 MathArray const & MathNestInset::cell(idx_type i) const
82 {
83         return cells_[i];
84 }
85
86
87 void MathNestInset::getCursorPos(CursorSlice const & cur,
88         int & x, int & y) const
89 {
90         BOOST_ASSERT(ptr_cmp(cur.inset(), this));
91         MathArray const & ar = cur.cell();
92         x = ar.xo() + ar.pos2x(cur.pos());
93         y = ar.yo();
94         // move cursor visually into empty cells ("blue rectangles");
95         if (cur.cell().empty())
96                 x += 2;
97 }
98
99
100 void MathNestInset::substitute(MathMacro const & m)
101 {
102         for (idx_type i = 0; i < nargs(); ++i)
103                 cell(i).substitute(m);
104 }
105
106
107 void MathNestInset::metrics(MetricsInfo const & mi) const
108 {
109         MetricsInfo m = mi;
110         for (idx_type i = 0; i < nargs(); ++i)
111                 cell(i).metrics(m);
112 }
113
114
115 bool MathNestInset::idxNext(LCursor & cur) const
116 {
117         BOOST_ASSERT(ptr_cmp(cur.inset(), this));
118         if (cur.idx() == cur.lastidx())
119                 return false;
120         ++cur.idx();
121         cur.pos() = 0;
122         return true;
123 }
124
125
126 bool MathNestInset::idxRight(LCursor & cur) const
127 {
128         return idxNext(cur);
129 }
130
131
132 bool MathNestInset::idxPrev(LCursor & cur) const
133 {
134         BOOST_ASSERT(ptr_cmp(cur.inset(), this));
135         if (cur.idx() == 0)
136                 return false;
137         --cur.idx();
138         cur.pos() = cur.lastpos();
139         return true;
140 }
141
142
143 bool MathNestInset::idxLeft(LCursor & cur) const
144 {
145         return idxPrev(cur);
146 }
147
148
149 bool MathNestInset::idxFirst(LCursor & cur) const
150 {
151         BOOST_ASSERT(ptr_cmp(cur.inset(), this));
152         if (nargs() == 0)
153                 return false;
154         cur.idx() = 0;
155         cur.pos() = 0;
156         return true;
157 }
158
159
160 bool MathNestInset::idxLast(LCursor & cur) const
161 {
162         BOOST_ASSERT(ptr_cmp(cur.inset(), this));
163         if (nargs() == 0)
164                 return false;
165         cur.idx() = cur.lastidx();
166         cur.pos() = cur.lastpos();
167         return true;
168 }
169
170
171 void MathNestInset::dump() const
172 {
173         WriteStream os(lyxerr);
174         os << "---------------------------------------------\n";
175         write(os);
176         os << "\n";
177         for (idx_type i = 0; i < nargs(); ++i)
178                 os << cell(i) << "\n";
179         os << "---------------------------------------------\n";
180 }
181
182
183 //void MathNestInset::draw(PainterInfo & pi, int x, int y) const
184 void MathNestInset::draw(PainterInfo &, int, int) const
185 {
186 #if 0
187         if (lock_)
188                 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
189                                         LColor::mathlockbg);
190 #endif
191 }
192
193
194 void MathNestInset::drawSelection(PainterInfo & pi, int, int) const
195 {
196         // this should use the x/y values given, not the cached values
197         LCursor & cur = pi.base.bv->cursor();
198         if (!cur.selection())
199                 return;
200         if (!ptr_cmp(cur.inset(), this))
201                 return;
202         CursorSlice & s1 = cur.selBegin();
203         CursorSlice & s2 = cur.selEnd();
204         if (s1.idx() == s2.idx()) {
205                 MathArray const & c = cell(s1.idx());
206                 int x1 = c.xo() + c.pos2x(s1.pos());
207                 int y1 = c.yo() - c.ascent();
208                 int x2 = c.xo() + c.pos2x(s2.pos());
209                 int y2 = c.yo() + c.descent();
210                 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
211         } else {
212                 for (idx_type i = 0; i < nargs(); ++i) {
213                         if (idxBetween(i, s1.idx(), s2.idx())) {
214                                 MathArray const & c = cell(i);
215                                 int x1 = c.xo();
216                                 int y1 = c.yo() - c.ascent();
217                                 int x2 = c.xo() + c.width();
218                                 int y2 = c.yo() + c.descent();
219                                 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
220                         }
221                 }
222         }
223 }
224
225
226 void MathNestInset::validate(LaTeXFeatures & features) const
227 {
228         for (idx_type i = 0; i < nargs(); ++i)
229                 cell(i).validate(features);
230 }
231
232
233 void MathNestInset::replace(ReplaceData & rep)
234 {
235         for (idx_type i = 0; i < nargs(); ++i)
236                 cell(i).replace(rep);
237 }
238
239
240 bool MathNestInset::contains(MathArray const & ar) const
241 {
242         for (idx_type i = 0; i < nargs(); ++i)
243                 if (cell(i).contains(ar))
244                         return true;
245         return false;
246 }
247
248
249 bool MathNestInset::lock() const
250 {
251         return lock_;
252 }
253
254
255 void MathNestInset::lock(bool l)
256 {
257         lock_ = l;
258 }
259
260
261 bool MathNestInset::isActive() const
262 {
263         return nargs() > 0;
264 }
265
266
267 MathArray MathNestInset::glue() const
268 {
269         MathArray ar;
270         for (size_t i = 0; i < nargs(); ++i)
271                 ar.append(cell(i));
272         return ar;
273 }
274
275
276 void MathNestInset::write(WriteStream & os) const
277 {
278         os << '\\' << name().c_str();
279         for (size_t i = 0; i < nargs(); ++i)
280                 os << '{' << cell(i) << '}';
281         if (nargs() == 0)
282                 os.pendingSpace(true);
283         if (lock_ && !os.latex()) {
284                 os << "\\lyxlock";
285                 os.pendingSpace(true);
286         }
287 }
288
289
290 void MathNestInset::normalize(NormalStream & os) const
291 {
292         os << '[' << name().c_str();
293         for (size_t i = 0; i < nargs(); ++i)
294                 os << ' ' << cell(i);
295         os << ']';
296 }
297
298
299 void MathNestInset::notifyCursorLeaves(idx_type idx)
300 {
301         cell(idx).notifyCursorLeaves();
302 }
303
304
305 void MathNestInset::handleFont
306         (LCursor & cur, string const & arg, string const & font)
307 {
308         // this whole function is a hack and won't work for incremental font
309         // changes...
310         //recordUndo(cur, Undo::ATOMIC);
311
312         if (cur.inset()->asMathInset()->name() == font)
313                 cur.handleFont(font);
314         else {
315                 cur.handleNest(createMathInset(font));
316                 cur.insert(arg);
317         }
318 }
319
320
321 void MathNestInset::handleFont2(LCursor & cur, string const & arg)
322 {
323         //recordUndo(cur, Undo::ATOMIC);
324         LyXFont font;
325         bool b;
326         bv_funcs::string2font(arg, font, b);
327         if (font.color() != LColor::inherit) {
328                 MathAtom at = createMathInset("color");
329                 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
330                 cur.handleNest(at, 1);
331         }
332 }
333
334
335 void MathNestInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
336 {
337         lyxerr << "MathNestInset: request: " << cmd << std::endl;
338         //CursorSlice sl = cur.current();
339
340         switch (cmd.action) {
341
342         case LFUN_PASTE:
343                 if (!cmd.argument.empty()) {
344                         MathArray ar;
345                         mathed_parse_cell(ar, cmd.argument);
346                         cur.cell().insert(cur.pos(), ar);
347                         cur.pos() += ar.size();
348                 }
349                 break;
350 /*
351         case LFUN_PASTE: {
352                 size_t n = 0;
353                 istringstream is(cmd.argument.c_str());
354                 is >> n;
355                 if (was_macro)
356                         cur.macroModeClose();
357                 //recordUndo(cur, Undo::ATOMIC);
358                 cur.selPaste(n);
359                 break;
360         }
361 */
362
363         case LFUN_PASTESELECTION:
364                 dispatch(cur, FuncRequest(LFUN_PASTE, cur.bv().getClipboard()));
365                 break;
366
367         case LFUN_MOUSE_PRESS:
368                 lfunMousePress(cur, cmd);
369                 break;
370
371         case LFUN_MOUSE_MOTION:
372                 lfunMouseMotion(cur, cmd);
373                 break;
374
375         case LFUN_MOUSE_RELEASE:
376                 lfunMouseRelease(cur, cmd);
377                 break;
378
379         case LFUN_MOUSE_DOUBLE:
380         case LFUN_MOUSE_TRIPLE:
381                 //lyxerr << "Mouse double" << endl;
382                 //lyxerr << "Mouse triple" << endl;
383                 dispatch(cur, FuncRequest(LFUN_WORDSEL));
384                 break;
385
386         case LFUN_FINISHED_LEFT:
387                 cur.pop(cur.currentDepth());
388                 cur.bv().cursor() = cur;
389                 break;
390
391         case LFUN_FINISHED_RIGHT:
392                 cur.pop(cur.currentDepth());
393                 ++cur.pos();
394                 cur.bv().cursor() = cur;
395                 break;
396
397         case LFUN_FINISHED_UP:
398                 cur.pop(cur.currentDepth());
399                 //idxUpDown(cur, true);
400                 cur.bv().cursor() = cur;
401                 break;
402
403         case LFUN_FINISHED_DOWN:
404                 cur.pop(cur.currentDepth());
405                 //idxUpDown(cur, false);
406                 cur.bv().cursor() = cur;
407                 break;
408
409         case LFUN_RIGHTSEL:
410         case LFUN_RIGHT:
411                 lyxerr << "mathnest RIGHT: from:\n" << cur << endl;
412                 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
413                 cur.autocorrect() = false;
414                 cur.clearTargetX();
415                 if (cur.inMacroMode())
416                         cur.macroModeClose();
417                 else if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
418                         cur.pushLeft(cur.nextAtom().nucleus());
419                         cur.inset()->idxFirst(cur);
420                 } else if (cur.posRight() || idxRight(cur)
421                         || cur.popRight() || cur.selection())
422                         ;
423                 else
424                         cur.dispatched(FINISHED_RIGHT);
425                 lyxerr << "mathnest RIGHT: to:\n" << cur << endl;
426                 break;
427
428         case LFUN_LEFTSEL:
429         case LFUN_LEFT:
430                 cur.selHandle(cmd.action == LFUN_LEFTSEL);
431                 cur.autocorrect() = false;
432                 cur.clearTargetX();
433                 if (cur.inMacroMode())
434                         cur.macroModeClose();
435                 else if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
436                         cur.posLeft();
437                         cur.push(cur.nextAtom().nucleus());
438                         cur.inset()->idxLast(cur);
439                 } else if (cur.posLeft() || idxLeft(cur)
440                         || cur.popLeft() || cur.selection())
441                         ;
442                 else
443                         cur.dispatched(FINISHED_LEFT);
444                 break;
445
446         case LFUN_UPSEL:
447         case LFUN_UP:
448                 cur.selHandle(cmd.action == LFUN_UPSEL);
449                 if (!cur.up())
450                         cur.dispatched(FINISHED_UP);
451                 break;
452
453         case LFUN_DOWNSEL:
454         case LFUN_DOWN:
455                 cur.selHandle(cmd.action == LFUN_DOWNSEL);
456                 if (!cur.down())
457                         cur.dispatched(FINISHED_DOWN);
458                 break;
459
460         case LFUN_WORDSEL:
461                 cur.pos() = 0;
462                 cur.idx() = 0;
463                 cur.resetAnchor();
464                 cur.selection() = true;
465                 cur.pos() = cur.lastpos();
466                 cur.idx() = cur.lastidx();
467                 break;
468
469         case LFUN_UP_PARAGRAPHSEL:
470         case LFUN_UP_PARAGRAPH:
471         case LFUN_DOWN_PARAGRAPHSEL:
472         case LFUN_DOWN_PARAGRAPH:
473                 break;
474
475         case LFUN_HOMESEL:
476         case LFUN_HOME:
477         case LFUN_WORDLEFTSEL:
478         case LFUN_WORDLEFT:
479                 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL || cmd.action == LFUN_HOMESEL);
480                 cur.macroModeClose();
481                 if (cur.pos() != 0) {
482                         cur.pos() = 0;
483                 } else if (cur.col() != 0) {
484                         cur.idx() -= cur.col();
485                         cur.pos() = 0;
486                 } else if (cur.idx() != 0) {
487                         cur.idx() = 0;
488                         cur.pos() = 0;
489                 } else {
490                         cur.dispatched(FINISHED_LEFT);
491                 }
492                 break;
493
494         case LFUN_WORDRIGHTSEL:
495         case LFUN_WORDRIGHT:
496         case LFUN_ENDSEL:
497         case LFUN_END:
498                 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL || cmd.action == LFUN_ENDSEL);
499                 cur.macroModeClose();
500                 cur.clearTargetX();
501                 if (cur.pos() != cur.lastpos()) {
502                         cur.pos() = cur.lastpos();
503                 } else if (cur.col() != cur.lastcol()) {
504                         cur.idx() = cur.idx() - cur.col() + cur.lastcol();
505                         cur.pos() = cur.lastpos();
506                 } else if (cur.idx() != cur.lastidx()) {
507                         cur.idx() = cur.lastidx();
508                         cur.pos() = cur.lastpos();
509                 } else {
510                         cur.dispatched(FINISHED_RIGHT);
511                 }
512                 break;
513
514         case LFUN_PRIORSEL:
515         case LFUN_PRIOR:
516         case LFUN_BEGINNINGBUFSEL:
517         case LFUN_BEGINNINGBUF:
518                 cur.dispatched(FINISHED_LEFT);
519                 break;
520
521         case LFUN_NEXTSEL:
522         case LFUN_NEXT:
523         case LFUN_ENDBUFSEL:
524         case LFUN_ENDBUF:
525                 cur.dispatched(FINISHED_RIGHT);
526                 break;
527
528         case LFUN_CELL_FORWARD:
529                 cur.inset()->idxNext(cur);
530                 break;
531
532         case LFUN_CELL_BACKWARD:
533                 cur.inset()->idxPrev(cur);
534                 break;
535
536         case LFUN_DELETE_WORD_BACKWARD:
537         case LFUN_BACKSPACE:
538                 //recordUndo(cur, Undo::ATOMIC);
539                 cur.backspace();
540                 break;
541
542         case LFUN_DELETE_WORD_FORWARD:
543         case LFUN_DELETE:
544                 //recordUndo(cur, Undo::ATOMIC);
545                 cur.erase();
546                 cur.dispatched(FINISHED_LEFT);
547                 break;
548
549         case LFUN_ESCAPE:
550                 if (cur.selection()) 
551                         cur.selClear();
552                 else 
553                         cur.dispatched(FINISHED_LEFT);
554                 break;
555
556         case LFUN_INSET_TOGGLE:
557                 cur.lockToggle();
558                 break;
559
560         case LFUN_SELFINSERT:
561                 if (cmd.argument.empty()) {
562                         cur.dispatched(FINISHED_RIGHT);
563                         break;
564                 }
565                 //recordUndo(cur, Undo::ATOMIC);
566                 if (cmd.argument.size() != 1) {
567                         cur.insert(cmd.argument);
568                         break;
569                 }
570                 if (!interpret(cur, cmd.argument[0]))
571                         cur.dispatched(FINISHED_RIGHT);
572                 break;
573
574 #if 0
575 //
576 // this needs to be incorporated
577 //
578         // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
579         bool remove_inset = false;
580
581         DispatchResult result(true);
582         bool was_macro     = cur.inMacroMode();
583
584         cur.normalize();
585         cur.touch();
586 #endif
587
588         //    case LFUN_GETXY:
589         //      sprintf(dispatch_buffer, "%d %d",);
590         //      DispatchResult= dispatch_buffer;
591         //      break;
592         case LFUN_SETXY: {
593                 lyxerr << "LFUN_SETXY broken!" << endl;
594                 int x = 0;
595                 int y = 0;
596                 istringstream is(cmd.argument.c_str());
597                 is >> x >> y;
598                 cur.setScreenPos(x, y);
599                 break;
600         }
601
602         case LFUN_CUT:
603                 //recordUndo(cur, Undo::DELETE);
604                 cur.selCut();
605                 break;
606
607         case LFUN_COPY:
608                 cur.selCopy();
609                 break;
610
611         // Special casing for superscript in case of LyX handling
612         // dead-keys:
613         case LFUN_CIRCUMFLEX:
614                 if (cmd.argument.empty()) {
615                         // do superscript if LyX handles
616                         // deadkeys
617                         //recordUndo(cur, Undo::ATOMIC);
618                         script(cur, true);
619                 }
620                 break;
621
622         case LFUN_UMLAUT:
623         case LFUN_ACUTE:
624         case LFUN_GRAVE:
625         case LFUN_BREVE:
626         case LFUN_DOT:
627         case LFUN_MACRON:
628         case LFUN_CARON:
629         case LFUN_TILDE:
630         case LFUN_CEDILLA:
631         case LFUN_CIRCLE:
632         case LFUN_UNDERDOT:
633         case LFUN_TIE:
634         case LFUN_OGONEK:
635         case LFUN_HUNG_UMLAUT:
636                 break;
637
638         //  Math fonts
639         case LFUN_FREEFONT_APPLY:
640         case LFUN_FREEFONT_UPDATE:
641                 handleFont2(cur, cmd.argument);
642                 break;
643
644         case LFUN_BOLD:
645                 handleFont(cur, cmd.argument, "mathbf");
646                 break;
647         case LFUN_SANS:
648                 handleFont(cur, cmd.argument, "mathsf");
649                 break;
650         case LFUN_EMPH:
651                 handleFont(cur, cmd.argument, "mathcal");
652                 break;
653         case LFUN_ROMAN:
654                 handleFont(cur, cmd.argument, "mathrm");
655                 break;
656         case LFUN_CODE:
657                 handleFont(cur, cmd.argument, "texttt");
658                 break;
659         case LFUN_FRAK:
660                 handleFont(cur, cmd.argument, "mathfrak");
661                 break;
662         case LFUN_ITAL:
663                 handleFont(cur, cmd.argument, "mathit");
664                 break;
665         case LFUN_NOUN:
666                 handleFont(cur, cmd.argument, "mathbb");
667                 break;
668         //case LFUN_FREEFONT_APPLY:
669                 handleFont(cur, cmd.argument, "textrm");
670                 break;
671         case LFUN_DEFAULT:
672                 handleFont(cur, cmd.argument, "textnormal");
673                 break;
674
675         case LFUN_MATH_MODE:
676 #if 1
677                 cur.macroModeClose();
678                 cur.selClearOrDel();
679                 cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
680                 cur.posLeft();
681                 cur.pushLeft(cur.nextInset());
682 #else
683                 if (currentMode() == InsetBase::TEXT_MODE)
684                         cur.niceInsert(MathAtom(new MathHullInset("simple")));
685                 else
686                         handleFont(cur, cmd.argument, "textrm");
687                 //cur.owner()->message(_("math text mode toggled"));
688 #endif
689                 break;
690
691         case LFUN_MATH_SIZE:
692 #if 0
693                 if (!arg.empty()) {
694                         //recordUndo(cur, Undo::ATOMIC);
695                         cur.setSize(arg);
696                 }
697 #endif
698                 break;
699
700         case LFUN_INSERT_MATRIX: {
701                 //recordUndo(cur, Undo::ATOMIC);
702                 unsigned int m = 1;
703                 unsigned int n = 1;
704                 string v_align;
705                 string h_align;
706                 istringstream is(cmd.argument);
707                 is >> m >> n >> v_align >> h_align;
708                 if (m < 1)
709                         m = 1;
710                 if (n < 1)
711                         n = 1;
712                 v_align += 'c';
713                 cur.niceInsert(
714                         MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
715                 break;
716         }
717
718         case LFUN_MATH_DELIM: {
719                 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
720                 string ls;
721                 string rs = lyx::support::split(cmd.argument, ls, ' ');
722                 // Reasonable default values
723                 if (ls.empty())
724                         ls = '(';
725                 if (rs.empty())
726                         rs = ')';
727                 //recordUndo(cur, Undo::ATOMIC);
728                 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
729                 break;
730         }
731
732         case LFUN_SPACE_INSERT:
733         case LFUN_MATH_SPACE:
734                 //recordUndo(cur, Undo::ATOMIC);
735                 cur.insert(MathAtom(new MathSpaceInset(",")));
736                 break;
737
738         case LFUN_UNDO:
739 #warning look here
740                 //cur.bv().owner()->message(_("Invalid action in math mode!"));
741                 break;
742
743         case LFUN_INSET_ERT:
744                 // interpret this as if a backslash was typed
745                 //recordUndo(cur, Undo::ATOMIC);
746                 interpret(cur, '\\');
747                 break;
748
749 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
750 // handling such that "self-insert" works on "arbitrary stuff" too, and
751 // math-insert only handles special math things like "matrix".
752         case LFUN_INSERT_MATH:
753                 //recordUndo(cur, Undo::ATOMIC);
754                 cur.niceInsert(cmd.argument);
755                 break;
756
757         case LFUN_DIALOG_SHOW_NEW_INSET: {
758                 string const & name = cmd.argument;
759                 string data;
760 #if 0
761                 if (name == "ref") {
762                         RefInset tmp(name);
763                         data = tmp.createDialogStr(name);
764                 }
765 #endif
766                 cur.bv().owner()->getDialogs().show(name, data, 0);
767                 break;
768         }
769
770         case LFUN_INSET_APPLY: {
771                 string const name = cmd.getArg(0);
772                 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
773
774                 if (base) {
775                         base->dispatch(cur, FuncRequest(LFUN_INSET_MODIFY, cmd.argument));
776                         break;
777                 }
778                 MathArray ar;
779                 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
780                         cur.insert(ar);
781                         break;
782                 }
783                 cur.notdispatched();
784                 break;
785         }
786
787 #warning look here
788 #if 0
789
790         case LFUN_WORD_REPLACE:
791         case LFUN_WORD_FIND:
792                 if (!searchForward(&cur.bv(), cmd.getArg(0), false, false))
793                         cur.notdispatched();
794                 break;
795
796         cur.normalize();
797         cur.touch();
798
799         BOOST_ASSERT(cur.inMathed());
800
801         if (result.dispatched()) {
802                 revealCodes(cur);
803                 cur.bv().stuffClipboard(cur.grabSelection());
804         } else {
805                 if (remove_inset)
806                         cur.bv().owner()->dispatch(FuncRequest(LFUN_DELETE));
807         }
808         break;
809 #endif
810
811         default:
812                 MathDimInset::priv_dispatch(cur, cmd);
813                 break;
814         }
815 }
816
817
818 void MathNestInset::edit(LCursor & cur, bool left)
819 {
820         cur.push(this);
821         cur.idx() = left ? 0 : cur.lastidx();
822         cur.pos() = left ? 0 : cur.lastpos();
823         cur.resetAnchor();
824 }
825
826
827 InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y)
828 {
829         int idx_min = 0;
830         int dist_min = 1000000;
831         for (idx_type i = 0; i < nargs(); ++i) {
832                 int d = cell(i).dist(x, y);
833                 if (d < dist_min) {
834                         dist_min = d;
835                         idx_min = i;
836                 }
837         }
838         MathArray & ar = cell(idx_min);
839         cur.push(this);
840         cur.idx() = idx_min;
841         cur.pos() = ar.x2pos(x - ar.xo());
842         lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
843         if (dist_min == 0) {
844                 // hit inside cell
845                 for (pos_type i = 0, n = ar.size(); i < n; ++i)
846                         if (ar[i]->covers(x, y))
847                                 return ar[i].nucleus()->editXY(cur, x, y);
848         }
849         return this;
850 }
851
852
853 void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
854 {
855         //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
856
857         if (cmd.button() == mouse_button::button1) {
858                 // try to dispatch to enclosed insets first
859                 //cur.bv().stuffClipboard(cur.grabSelection());
860                 return;
861         }
862
863         if (cmd.button() == mouse_button::button2) {
864                 MathArray ar;
865                 asArray(cur.bv().getClipboard(), ar);
866                 cur.selClear();
867                 cur.setScreenPos(cmd.x, cmd.y);
868                 cur.insert(ar);
869                 cur.bv().update();
870                 return;
871         }
872
873         if (cmd.button() == mouse_button::button3) {
874                 // try to dispatch to enclosed insets first
875                 cur.bv().owner()->getDialogs().show("mathpanel");
876                 return;
877         }
878
879         cur.notdispatched();
880 }
881
882
883 void MathNestInset::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
884 {
885         lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
886         if (cmd.button() == mouse_button::button1) {
887                 first_x = cmd.x;
888                 first_y = cmd.y;
889                 cur.selClear();
890                 //cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
891                 lyxerr << "lfunMousePress: setting cursor to: " << cur << endl;
892                 cur.bv().cursor() = cur;
893         }
894
895         if (cmd.button() == mouse_button::button2) {
896                 priv_dispatch(cur, FuncRequest(LFUN_PASTESELECTION));
897         }
898 }
899
900
901 void MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
902 {
903         // only select with button 1
904         if (cmd.button() != mouse_button::button1)
905                 return;
906
907         if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
908                 return;
909
910         first_x = cmd.x;
911         first_y = cmd.y;
912
913         if (!cur.selection())
914                 cur.selBegin();
915
916         //cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
917         cur.bv().cursor().cursor_ = cur.cursor_;
918         cur.bv().cursor().selection() = true;
919         return;
920 }
921
922
923 bool MathNestInset::interpret(LCursor & cur, char c)
924 {
925         //lyxerr << "interpret 2: '" << c << "'" << endl;
926         cur.clearTargetX();
927         if (cur.inMacroArgMode()) {
928                 cur.posLeft();
929                 cur.plainErase();
930 #warning FIXME
931 #if 0
932                 int n = c - '0';
933                 MathMacroTemplate const * p = formula()->asMacroTemplate();
934                 if (p && 1 <= n && n <= p->numargs())
935                         cur.insert(MathAtom(new MathMacroArgument(c - '0')));
936                 else {
937                         cur.insert(createMathInset("#"));
938                         interpret(cur, c); // try again
939                 }
940 #endif
941                 return true;
942         }
943
944         // handle macroMode
945         if (cur.inMacroMode()) {
946                 string name = cur.macroName();
947                 //lyxerr << "interpret name: '" << name << "'" << endl;
948
949                 if (isalpha(c)) {
950                         cur.activeMacro()->setName(cur.activeMacro()->name() + c);
951                         return true;
952                 }
953
954                 // handle 'special char' macros
955                 if (name == "\\") {
956                         // remove the '\\'
957                         cur.backspace();
958                         if (c == '\\') {
959                                 if (currentMode() == MathInset::TEXT_MODE)
960                                         cur.niceInsert(createMathInset("textbackslash"));
961                                 else
962                                         cur.niceInsert(createMathInset("backslash"));
963                         } else if (c == '{') {
964                                 cur.niceInsert(MathAtom(new MathBraceInset));
965                         } else {
966                                 cur.niceInsert(createMathInset(string(1, c)));
967                         }
968                         return true;
969                 }
970
971                 // leave macro mode and try again if necessary
972                 cur.macroModeClose();
973                 if (c == '{')
974                         cur.niceInsert(MathAtom(new MathBraceInset));
975                 else if (c != ' ')
976                         interpret(cur, c);
977                 return true;
978         }
979
980         // This is annoying as one has to press <space> far too often.
981         // Disable it.
982
983 #if 0
984                 // leave autocorrect mode if necessary
985                 if (autocorrect() && c == ' ') {
986                         autocorrect() = false;
987                         return true;
988                 }
989 #endif
990
991         // just clear selection on pressing the space bar
992         if (cur.selection() && c == ' ') {
993                 cur.selection() = false;
994                 return true;
995         }
996
997         cur.selClearOrDel();
998
999         if (c == '\\') {
1000                 //lyxerr << "starting with macro" << endl;
1001                 cur.insert(MathAtom(new MathUnknownInset("\\", false)));
1002                 return true;
1003         }
1004
1005         if (c == '\n') {
1006                 if (currentMode() == MathInset::TEXT_MODE)
1007                         cur.insert(c);
1008                 return true;
1009         }
1010
1011         if (c == ' ') {
1012                 if (currentMode() == MathInset::TEXT_MODE) {
1013                         // insert spaces in text mode,
1014                         // but suppress direct insertion of two spaces in a row
1015                         // the still allows typing  '<space>a<space>' and deleting the 'a', but
1016                         // it is better than nothing...
1017                         if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1018                                 cur.insert(c);
1019                         return true;
1020                 }
1021                 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1022                         cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1023                         return true;
1024                 }
1025                 if (cur.popRight())
1026                         return true;
1027                 // if are at the very end, leave the formula
1028                 return cur.pos() != cur.lastpos();
1029         }
1030
1031         if (c == '_') {
1032                 script(cur, false);
1033                 return true;
1034         }
1035
1036         if (c == '^') {
1037                 script(cur, true);
1038                 return true;
1039         }
1040
1041         if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
1042                 cur.niceInsert(createMathInset(string(1, c)));
1043                 return true;
1044         }
1045
1046         if (c == '%') {
1047                 cur.niceInsert(MathAtom(new MathCommentInset));
1048                 return true;
1049         }
1050
1051         // try auto-correction
1052         //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1053         //      return true;
1054
1055         // no special circumstances, so insert the character without any fuss
1056         cur.insert(c);
1057         cur.autocorrect() = true;
1058         return true;
1059 }
1060
1061
1062 bool MathNestInset::script(LCursor & cur, bool up)
1063 {
1064         // Hack to get \\^ and \\_ working
1065         lyxerr << "handling script: up: " << up << endl;
1066         if (cur.inMacroMode() && cur.macroName() == "\\") {
1067                 if (up)
1068                         cur.niceInsert(createMathInset("mathcircumflex"));
1069                 else
1070                         interpret(cur, '_');
1071                 return true;
1072         }
1073
1074         cur.macroModeClose();
1075         string safe = cur.grabAndEraseSelection();
1076         if (asScriptInset() && cur.idx() == 2) {
1077                 // we are in a nucleus of a script inset, move to _our_ script
1078                 asScriptInset()->ensure(up);
1079                 cur.idx() = up;
1080                 cur.pos() = 0;
1081         } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1082                 --cur.pos();
1083                 cur.nextAtom().nucleus()->asScriptInset()->ensure(up);
1084                 cur.push(cur.nextInset());
1085                 cur.idx() = up;
1086                 cur.pos() = cur.lastpos();
1087         } else if (cur.pos() != 0) {
1088                 --cur.pos();
1089                 cur.cell()[cur.pos()] = MathAtom(new MathScriptInset(cur.nextAtom(), up));
1090                 cur.push(cur.nextInset());
1091                 cur.idx() = up;
1092                 cur.pos() = 0;
1093         } else {
1094                 cur.plainInsert(MathAtom(new MathScriptInset(up)));
1095                 --cur.pos();
1096                 cur.nextAtom().nucleus()->asScriptInset()->ensure(up);
1097                 cur.push(cur.nextInset());
1098                 cur.idx() = up;
1099                 cur.pos() = 0;
1100         }
1101         cur.paste(safe);
1102         return true;
1103 }