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