]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
e67917f34661a4a59d3e71035bea7fbdba0de0de
[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 #warning look here
319 #if 0
320         MathArray & ar = cur.cell();
321         // remove base-only "scripts"
322         for (pos_type i = 0; i + 1 < ar.size(); ++i) {
323                 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
324                 if (p && p->nargs() == 1) {
325                         MathArray ar = p->nuc();
326                         erase(i);
327                         insert(i, ar);
328                         cur.adjust(i, ar.size() - 1);
329                 }
330         }
331
332         // glue adjacent font insets of the same kind
333         for (pos_type i = 0; i + 1 < size(); ++i) {
334                 MathFontInset * p = operator[](i).nucleus()->asFontInset();
335                 MathFontInset const * q = operator[](i + 1)->asFontInset();
336                 if (p && q && p->name() == q->name()) {
337                         p->cell(0).append(q->cell(0));
338                         erase(i + 1);
339                         cur.adjust(i, -1);
340                 }
341         }
342 #endif
343 }
344
345
346 void MathNestInset::handleFont
347         (LCursor & cur, string const & arg, string const & font)
348 {
349         // this whole function is a hack and won't work for incremental font
350         // changes...
351         recordUndo(cur, Undo::ATOMIC);
352
353         if (cur.inset().asMathInset()->name() == font)
354                 cur.handleFont(font);
355         else {
356                 cur.handleNest(createMathInset(font));
357                 cur.insert(arg);
358         }
359 }
360
361
362 void MathNestInset::handleFont2(LCursor & cur, string const & arg)
363 {
364         recordUndo(cur, Undo::ATOMIC);
365         LyXFont font;
366         bool b;
367         bv_funcs::string2font(arg, font, b);
368         if (font.color() != LColor::inherit) {
369                 MathAtom at = createMathInset("color");
370                 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
371                 cur.handleNest(at, 1);
372         }
373 }
374
375
376 void MathNestInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
377 {
378         //lyxerr << "MathNestInset: request: " << cmd << std::endl;
379         //CursorSlice sl = cur.current();
380
381         switch (cmd.action) {
382
383         case LFUN_PASTE: {
384                 cur.message(_("Paste"));
385                 replaceSelection(cur);
386                 size_t n = 0;
387                 istringstream is(cmd.argument.c_str());
388                 is >> n;
389                 pasteSelection(cur, n);
390                 cur.clearSelection(); // bug 393
391                 cur.bv().switchKeyMap();
392                 finishUndo();
393                 break;
394         }
395
396         case LFUN_CUT:
397                 cutSelection(cur, true, true);
398                 cur.message(_("Cut"));
399                 break;
400
401         case LFUN_COPY:
402                 copySelection(cur);
403                 cur.message(_("Copy"));
404                 break;
405
406         case LFUN_MOUSE_PRESS:
407                 lfunMousePress(cur, cmd);
408                 break;
409
410         case LFUN_MOUSE_MOTION:
411                 lfunMouseMotion(cur, cmd);
412                 break;
413
414         case LFUN_MOUSE_RELEASE:
415                 lfunMouseRelease(cur, cmd);
416                 break;
417
418         case LFUN_FINISHED_LEFT:
419                 cur.bv().cursor() = cur;
420                 break;
421
422         case LFUN_FINISHED_RIGHT:
423                 ++cur.pos();
424                 cur.bv().cursor() = cur;
425                 break;
426
427         case LFUN_FINISHED_UP:
428                 //idxUpDown(cur, true);
429                 cur.bv().cursor() = cur;
430                 break;
431
432         case LFUN_FINISHED_DOWN:
433                 //idxUpDown(cur, false);
434                 cur.bv().cursor() = cur;
435                 break;
436
437         case LFUN_RIGHTSEL:
438         case LFUN_RIGHT:
439                 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
440                 cur.autocorrect() = false;
441                 cur.clearTargetX();
442                 if (cur.inMacroMode())
443                         cur.macroModeClose();
444                 else if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
445                         cur.pushLeft(*cur.nextAtom().nucleus());
446                         cur.inset().idxFirst(cur);
447                 } else if (cur.posRight() || idxRight(cur)
448                         || cur.popRight() || cur.selection())
449                         ;
450                 else
451                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
452                 break;
453
454         case LFUN_LEFTSEL:
455         case LFUN_LEFT:
456                 cur.selHandle(cmd.action == LFUN_LEFTSEL);
457                 cur.autocorrect() = false;
458                 cur.clearTargetX();
459                 if (cur.inMacroMode())
460                         cur.macroModeClose();
461                 else if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
462                         cur.posLeft();
463                         cur.push(*cur.nextAtom().nucleus());
464                         cur.inset().idxLast(cur);
465                 } else if (cur.posLeft() || idxLeft(cur)
466                         || cur.popLeft() || cur.selection())
467                         ;
468                 else
469                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
470                 break;
471
472         case LFUN_UPSEL:
473         case LFUN_UP:
474                 cur.selHandle(cmd.action == LFUN_UPSEL);
475                 if (!cur.up())
476                         cmd = FuncRequest(LFUN_FINISHED_UP);
477                 break;
478
479         case LFUN_DOWNSEL:
480         case LFUN_DOWN:
481                 cur.selHandle(cmd.action == LFUN_DOWNSEL);
482                 if (!cur.down())
483                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
484                 break;
485
486         case LFUN_MOUSE_DOUBLE:
487         case LFUN_MOUSE_TRIPLE:
488         case LFUN_WORDSEL:
489                 cur.pos() = 0;
490                 cur.idx() = 0;
491                 cur.resetAnchor();
492                 cur.selection() = true;
493                 cur.pos() = cur.lastpos();
494                 cur.idx() = cur.lastidx();
495                 break;
496
497         case LFUN_UP_PARAGRAPHSEL:
498         case LFUN_UP_PARAGRAPH:
499         case LFUN_DOWN_PARAGRAPHSEL:
500         case LFUN_DOWN_PARAGRAPH:
501                 break;
502
503         case LFUN_HOMESEL:
504         case LFUN_HOME:
505         case LFUN_WORDLEFTSEL:
506         case LFUN_WORDLEFT:
507                 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL || cmd.action == LFUN_HOMESEL);
508                 cur.macroModeClose();
509                 if (cur.pos() != 0) {
510                         cur.pos() = 0;
511                 } else if (cur.col() != 0) {
512                         cur.idx() -= cur.col();
513                         cur.pos() = 0;
514                 } else if (cur.idx() != 0) {
515                         cur.idx() = 0;
516                         cur.pos() = 0;
517                 } else {
518                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
519                 }
520                 break;
521
522         case LFUN_WORDRIGHTSEL:
523         case LFUN_WORDRIGHT:
524         case LFUN_ENDSEL:
525         case LFUN_END:
526                 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL || cmd.action == LFUN_ENDSEL);
527                 cur.macroModeClose();
528                 cur.clearTargetX();
529                 if (cur.pos() != cur.lastpos()) {
530                         cur.pos() = cur.lastpos();
531                 } else if (cur.col() != cur.lastcol()) {
532                         cur.idx() = cur.idx() - cur.col() + cur.lastcol();
533                         cur.pos() = cur.lastpos();
534                 } else if (cur.idx() != cur.lastidx()) {
535                         cur.idx() = cur.lastidx();
536                         cur.pos() = cur.lastpos();
537                 } else {
538                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
539                 }
540                 break;
541
542         case LFUN_PRIORSEL:
543         case LFUN_PRIOR:
544         case LFUN_BEGINNINGBUFSEL:
545         case LFUN_BEGINNINGBUF:
546                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
547                 break;
548
549         case LFUN_NEXTSEL:
550         case LFUN_NEXT:
551         case LFUN_ENDBUFSEL:
552         case LFUN_ENDBUF:
553                 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
554                 break;
555
556         case LFUN_CELL_FORWARD:
557                 cur.inset().idxNext(cur);
558                 break;
559
560         case LFUN_CELL_BACKWARD:
561                 cur.inset().idxPrev(cur);
562                 break;
563
564         case LFUN_DELETE_WORD_BACKWARD:
565         case LFUN_BACKSPACE:
566                 recordUndo(cur, Undo::ATOMIC);
567                 cur.backspace();
568                 break;
569
570         case LFUN_DELETE_WORD_FORWARD:
571         case LFUN_DELETE:
572                 recordUndo(cur, Undo::ATOMIC);
573                 cur.erase();
574                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
575                 break;
576
577         case LFUN_ESCAPE:
578                 if (cur.selection())
579                         cur.clearSelection();
580                 else
581                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
582                 break;
583
584         case LFUN_INSET_TOGGLE:
585                 //lockToggle();
586                 if (cur.pos() != cur.lastpos()) {
587                         // toggle previous inset ...
588                         cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
589                 } else if (cur.popLeft() && cur.pos() != cur.lastpos()) {
590                         // ... or enclosing inset if we are in the last inset position
591                         cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
592                         ++cur.pos();
593                 }
594                 break;
595
596         case LFUN_SELFINSERT:
597                 if (cmd.argument.empty()) {
598                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
599                         break;
600                 }
601                 recordUndo(cur, Undo::ATOMIC);
602                 if (cmd.argument.size() != 1) {
603                         cur.insert(cmd.argument);
604                         break;
605                 }
606                 if (!interpret(cur, cmd.argument[0]))
607                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
608                 break;
609
610 #if 0
611 //
612 // this needs to be incorporated
613 //
614         // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
615         bool remove_inset = false;
616
617         DispatchResult result(true);
618         bool was_macro = cur.inMacroMode();
619
620         cur.normalize();
621         cur.touch();
622 #endif
623
624         //    case LFUN_GETXY:
625         //      sprintf(dispatch_buffer, "%d %d",);
626         //      DispatchResult= dispatch_buffer;
627         //      break;
628         case LFUN_SETXY: {
629                 lyxerr << "LFUN_SETXY broken!" << endl;
630                 int x = 0;
631                 int y = 0;
632                 istringstream is(cmd.argument.c_str());
633                 is >> x >> y;
634                 cur.setScreenPos(x, y);
635                 break;
636         }
637
638         // Special casing for superscript in case of LyX handling
639         // dead-keys:
640         case LFUN_CIRCUMFLEX:
641                 if (cmd.argument.empty()) {
642                         // do superscript if LyX handles
643                         // deadkeys
644                         recordUndo(cur, Undo::ATOMIC);
645                         script(cur, true);
646                 }
647                 break;
648
649         case LFUN_UMLAUT:
650         case LFUN_ACUTE:
651         case LFUN_GRAVE:
652         case LFUN_BREVE:
653         case LFUN_DOT:
654         case LFUN_MACRON:
655         case LFUN_CARON:
656         case LFUN_TILDE:
657         case LFUN_CEDILLA:
658         case LFUN_CIRCLE:
659         case LFUN_UNDERDOT:
660         case LFUN_TIE:
661         case LFUN_OGONEK:
662         case LFUN_HUNG_UMLAUT:
663                 break;
664
665         //  Math fonts
666         case LFUN_FREEFONT_APPLY:
667         case LFUN_FREEFONT_UPDATE:
668                 handleFont2(cur, cmd.argument);
669                 break;
670
671         case LFUN_BOLD:
672                 handleFont(cur, cmd.argument, "mathbf");
673                 break;
674         case LFUN_SANS:
675                 handleFont(cur, cmd.argument, "mathsf");
676                 break;
677         case LFUN_EMPH:
678                 handleFont(cur, cmd.argument, "mathcal");
679                 break;
680         case LFUN_ROMAN:
681                 handleFont(cur, cmd.argument, "mathrm");
682                 break;
683         case LFUN_CODE:
684                 handleFont(cur, cmd.argument, "texttt");
685                 break;
686         case LFUN_FRAK:
687                 handleFont(cur, cmd.argument, "mathfrak");
688                 break;
689         case LFUN_ITAL:
690                 handleFont(cur, cmd.argument, "mathit");
691                 break;
692         case LFUN_NOUN:
693                 handleFont(cur, cmd.argument, "mathbb");
694                 break;
695         //case LFUN_FREEFONT_APPLY:
696                 handleFont(cur, cmd.argument, "textrm");
697                 break;
698         case LFUN_DEFAULT:
699                 handleFont(cur, cmd.argument, "textnormal");
700                 break;
701
702         case LFUN_MATH_MODE:
703 #if 1
704                 cur.macroModeClose();
705                 selClearOrDel(cur);
706                 cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
707                 cur.posLeft();
708                 cur.pushLeft(*cur.nextInset());
709 #else
710                 if (currentMode() == InsetBase::TEXT_MODE)
711                         cur.niceInsert(MathAtom(new MathHullInset("simple")));
712                 else
713                         handleFont(cur, cmd.argument, "textrm");
714                 //cur.owner()->message(_("math text mode toggled"));
715 #endif
716                 break;
717
718         case LFUN_MATH_SIZE:
719 #if 0
720                 if (!arg.empty()) {
721                         recordUndo(cur, Undo::ATOMIC);
722                         cur.setSize(arg);
723                 }
724 #endif
725                 break;
726
727         case LFUN_INSERT_MATRIX: {
728                 recordUndo(cur, Undo::ATOMIC);
729                 unsigned int m = 1;
730                 unsigned int n = 1;
731                 string v_align;
732                 string h_align;
733                 istringstream is(cmd.argument);
734                 is >> m >> n >> v_align >> h_align;
735                 if (m < 1)
736                         m = 1;
737                 if (n < 1)
738                         n = 1;
739                 v_align += 'c';
740                 cur.niceInsert(
741                         MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
742                 break;
743         }
744
745         case LFUN_MATH_DELIM: {
746                 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
747                 string ls;
748                 string rs = lyx::support::split(cmd.argument, ls, ' ');
749                 // Reasonable default values
750                 if (ls.empty())
751                         ls = '(';
752                 if (rs.empty())
753                         rs = ')';
754                 recordUndo(cur, Undo::ATOMIC);
755                 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
756                 break;
757         }
758
759         case LFUN_SPACE_INSERT:
760         case LFUN_MATH_SPACE:
761                 recordUndo(cur, Undo::ATOMIC);
762                 cur.insert(MathAtom(new MathSpaceInset(",")));
763                 break;
764
765         case LFUN_INSET_ERT:
766                 // interpret this as if a backslash was typed
767                 recordUndo(cur, Undo::ATOMIC);
768                 interpret(cur, '\\');
769                 break;
770
771 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
772 // handling such that "self-insert" works on "arbitrary stuff" too, and
773 // math-insert only handles special math things like "matrix".
774         case LFUN_INSERT_MATH:
775                 recordUndo(cur, Undo::ATOMIC);
776                 cur.niceInsert(cmd.argument);
777                 break;
778
779         case LFUN_DIALOG_SHOW_NEW_INSET: {
780                 string const & name = cmd.argument;
781                 string data;
782 #if 0
783                 if (name == "ref") {
784                         RefInset tmp(name);
785                         data = tmp.createDialogStr(name);
786                 }
787 #endif
788                 cur.bv().owner()->getDialogs().show(name, data, 0);
789                 break;
790         }
791
792         case LFUN_INSET_APPLY: {
793                 string const name = cmd.getArg(0);
794                 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
795
796                 if (base) {
797                         FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
798                         base->dispatch(cur, fr);
799                         break;
800                 }
801                 MathArray ar;
802                 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
803                         cur.insert(ar);
804                         break;
805                 }
806                 cur.undispatched();
807                 break;
808         }
809
810 #ifdef WITH_WARNINGS
811 #warning look here
812 #endif
813 #if 0
814
815         case LFUN_WORD_REPLACE:
816         case LFUN_WORD_FIND:
817                 if (!searchForward(&cur.bv(), cmd.getArg(0), false, false))
818                         cur.undispatched();
819                 break;
820
821         cur.normalize();
822         cur.touch();
823
824         BOOST_ASSERT(cur.inMathed());
825
826         if (result.dispatched()) {
827                 revealCodes(cur);
828                 cur.bv().stuffClipboard(cur.grabSelection());
829         } else {
830                 if (remove_inset)
831                         cur.bv().owner()->dispatch(FuncRequest(LFUN_DELETE));
832         }
833         break;
834 #endif
835
836         default:
837                 MathDimInset::priv_dispatch(cur, cmd);
838                 break;
839         }
840 }
841
842
843 bool MathNestInset::getStatus(LCursor & /*cur*/, FuncRequest const & cmd,
844                 FuncStatus & flag) const
845 {
846         // the font related toggles
847         //string tc = "mathnormal"; 
848         bool ret = true;
849         switch (cmd.action) {
850 #if 0
851         case LFUN_TABULAR_FEATURE:
852                 // FIXME: check temporarily disabled
853                 // valign code
854                 char align = mathcursor::valign();
855                 if (align == '\0') {
856                         enable = false;
857                         break;
858                 }
859                 if (cmd.argument.empty()) {
860                         flag.clear();
861                         break;
862                 }
863                 if (!contains("tcb", cmd.argument[0])) {
864                         enable = false;
865                         break;
866                 }
867                 flag.setOnOff(cmd.argument[0] == align);
868                 break;
869         case LFUN_BOLD:
870                 flag.setOnOff(tc == "mathbf");
871                 break;
872         case LFUN_SANS:
873                 flag.setOnOff(tc == "mathsf");
874                 break;
875         case LFUN_EMPH:
876                 flag.setOnOff(tc == "mathcal");
877                 break;
878         case LFUN_ROMAN:
879                 flag.setOnOff(tc == "mathrm");
880                 break;
881         case LFUN_CODE:
882                 flag.setOnOff(tc == "mathtt");
883                 break;
884         case LFUN_NOUN:
885                 flag.setOnOff(tc == "mathbb");
886                 break;
887         case LFUN_DEFAULT:
888                 flag.setOnOff(tc == "mathnormal");
889                 break;
890 #endif
891         case LFUN_MATH_MUTATE:
892                 //flag.setOnOff(mathcursor::formula()->hullType() == cmd.argument);
893                 flag.setOnOff(false);
894                 break;
895
896         // we just need to be in math mode to enable that
897         case LFUN_MATH_SIZE:
898         case LFUN_MATH_SPACE:
899         case LFUN_MATH_LIMITS:
900         case LFUN_MATH_NONUMBER:
901         case LFUN_MATH_NUMBER:
902         case LFUN_MATH_EXTERN:
903                 flag.enabled(true);
904                 break;
905
906         default:
907                 ret = false;
908                 break;
909         }
910         return ret;
911 }
912
913
914 void MathNestInset::edit(LCursor & cur, bool left)
915 {
916         cur.push(*this);
917         cur.idx() = left ? 0 : cur.lastidx();
918         cur.pos() = left ? 0 : cur.lastpos();
919         cur.resetAnchor();
920         lyxerr << "MathNestInset::edit, cur:\n" << cur << endl;
921 }
922
923
924 InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y)
925 {
926         int idx_min = 0;
927         int dist_min = 1000000;
928         for (idx_type i = 0; i < nargs(); ++i) {
929                 int d = cell(i).dist(x, y);
930                 if (d < dist_min) {
931                         dist_min = d;
932                         idx_min = i;
933                 }
934         }
935         MathArray & ar = cell(idx_min);
936         cur.push(*this);
937         cur.idx() = idx_min;
938         cur.pos() = ar.x2pos(x - ar.xo());
939         lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
940         if (dist_min == 0) {
941                 // hit inside cell
942                 for (pos_type i = 0, n = ar.size(); i < n; ++i)
943                         if (ar[i]->covers(x, y))
944                                 return ar[i].nucleus()->editXY(cur, x, y);
945         }
946         return this;
947 }
948
949
950 void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
951 {
952         //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
953
954         if (cmd.button() == mouse_button::button1) {
955                 // try to dispatch to enclosed insets first
956                 //cur.bv().stuffClipboard(cur.grabSelection());
957                 return;
958         }
959
960         if (cmd.button() == mouse_button::button2) {
961                 MathArray ar;
962                 asArray(cur.bv().getClipboard(), ar);
963                 cur.clearSelection();
964                 cur.setScreenPos(cmd.x, cmd.y);
965                 cur.insert(ar);
966                 cur.bv().update();
967                 return;
968         }
969
970         if (cmd.button() == mouse_button::button3) {
971                 // try to dispatch to enclosed insets first
972                 cur.bv().owner()->getDialogs().show("mathpanel");
973                 return;
974         }
975
976         cur.undispatched();
977 }
978
979
980 void MathNestInset::lfunMousePress(LCursor & cur, FuncRequest & cmd)
981 {
982         lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
983         if (cmd.button() == mouse_button::button1) {
984                 first_x = cmd.x;
985                 first_y = cmd.y;
986                 //cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
987                 lyxerr << "lfunMousePress: setting cursor to: " << cur << endl;
988                 cur.resetAnchor();
989                 cur.bv().cursor() = cur;
990         }
991
992         if (cmd.button() == mouse_button::button2) {
993                 cur.dispatch(FuncRequest(LFUN_PASTESELECTION));
994         }
995 }
996
997
998 void MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
999 {
1000         // only select with button 1
1001         if (cmd.button() != mouse_button::button1)
1002                 return;
1003
1004         if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
1005                 return;
1006
1007         first_x = cmd.x;
1008         first_y = cmd.y;
1009
1010         cur.bv().cursor().setCursor(cur, true);
1011 }
1012
1013
1014 bool MathNestInset::interpret(LCursor & cur, char c)
1015 {
1016         lyxerr << "interpret 2: '" << c << "'" << endl;
1017         cur.clearTargetX();
1018
1019         // handle macroMode
1020         if (cur.inMacroMode()) {
1021                 string name = cur.macroName();
1022                 lyxerr << "interpret macro name: '" << name << "'" << endl;
1023
1024                 /// are we currently typing '#1' or '#2' or...?
1025                 if (name == "\\#") {
1026                         cur.backspace();
1027                         int n = c - '0';
1028                         if (n >= 1 && n <= 9)
1029                                 cur.insert(new MathMacroArgument(n));
1030                         return true;
1031                 }
1032
1033                 if (isalpha(c)) {
1034                         cur.activeMacro()->setName(name + c);
1035                         return true;
1036                 }
1037
1038                 // handle 'special char' macros
1039                 if (name == "\\") {
1040                         // remove the '\\'
1041                         if (c == '\\') {
1042                                 cur.backspace();
1043                                 if (currentMode() == MathInset::TEXT_MODE)
1044                                         cur.niceInsert(createMathInset("textbackslash"));
1045                                 else
1046                                         cur.niceInsert(createMathInset("backslash"));
1047                         } else if (c == '{') {
1048                                 cur.backspace();
1049                                 cur.niceInsert(MathAtom(new MathBraceInset));
1050                         } else if (c == '#') {
1051                                 lyxerr << "setting name to " << name + c << endl;
1052                                 BOOST_ASSERT(cur.activeMacro());
1053                                 cur.activeMacro()->setName(name + c);
1054                                 lyxerr << "set name to " << name + c << endl;
1055                         } else {
1056                                 cur.backspace();
1057                                 cur.niceInsert(createMathInset(string(1, c)));
1058                         }
1059                         return true;
1060                 }
1061
1062                 // leave macro mode and try again if necessary
1063                 cur.macroModeClose();
1064                 if (c == '{')
1065                         cur.niceInsert(MathAtom(new MathBraceInset));
1066                 else if (c != ' ')
1067                         interpret(cur, c);
1068                 return true;
1069         }
1070
1071         // This is annoying as one has to press <space> far too often.
1072         // Disable it.
1073
1074 #if 0
1075                 // leave autocorrect mode if necessary
1076                 if (autocorrect() && c == ' ') {
1077                         autocorrect() = false;
1078                         return true;
1079                 }
1080 #endif
1081
1082         // just clear selection on pressing the space bar
1083         if (cur.selection() && c == ' ') {
1084                 cur.selection() = false;
1085                 return true;
1086         }
1087
1088         selClearOrDel(cur);
1089
1090         if (c == '\\') {
1091                 //lyxerr << "starting with macro" << endl;
1092                 cur.insert(MathAtom(new MathUnknownInset("\\", false)));
1093                 return true;
1094         }
1095
1096         if (c == '\n') {
1097                 if (currentMode() == MathInset::TEXT_MODE)
1098                         cur.insert(c);
1099                 return true;
1100         }
1101
1102         if (c == ' ') {
1103                 if (currentMode() == MathInset::TEXT_MODE) {
1104                         // insert spaces in text mode,
1105                         // but suppress direct insertion of two spaces in a row
1106                         // the still allows typing  '<space>a<space>' and deleting the 'a', but
1107                         // it is better than nothing...
1108                         if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1109                                 cur.insert(c);
1110                         return true;
1111                 }
1112                 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1113                         cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1114                         return true;
1115                 }
1116                 if (cur.popRight())
1117                         return true;
1118                 // if are at the very end, leave the formula
1119                 return cur.pos() != cur.lastpos();
1120         }
1121
1122         if (c == '_') {
1123                 script(cur, false);
1124                 return true;
1125         }
1126
1127         if (c == '^') {
1128                 script(cur, true);
1129                 return true;
1130         }
1131
1132         if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#') {
1133                 cur.niceInsert(createMathInset(string(1, c)));
1134                 return true;
1135         }
1136
1137         if (c == '%') {
1138                 cur.niceInsert(MathAtom(new MathCommentInset));
1139                 return true;
1140         }
1141
1142         // try auto-correction
1143         //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1144         //      return true;
1145
1146         // no special circumstances, so insert the character without any fuss
1147         cur.insert(c);
1148         cur.autocorrect() = true;
1149         return true;
1150 }
1151
1152
1153 bool MathNestInset::script(LCursor & cur, bool up)
1154 {
1155         // Hack to get \\^ and \\_ working
1156         lyxerr << "handling script: up: " << up << endl;
1157         if (cur.inMacroMode() && cur.macroName() == "\\") {
1158                 if (up)
1159                         cur.niceInsert(createMathInset("mathcircumflex"));
1160                 else
1161                         interpret(cur, '_');
1162                 return true;
1163         }
1164
1165         cur.macroModeClose();
1166         string safe = grabAndEraseSelection(cur);
1167         if (asScriptInset() && cur.idx() == 0) {
1168                 // we are in a nucleus of a script inset, move to _our_ script
1169                 MathScriptInset * inset = asScriptInset();
1170                 lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1171                 inset->ensure(up);
1172                 cur.idx() = inset->idxOfScript(up);
1173                 cur.pos() = 0;
1174         } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1175                 --cur.pos();
1176                 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1177                 cur.push(*inset);
1178                 cur.idx() = inset->idxOfScript(up);
1179                 cur.pos() = cur.lastpos();
1180         } else {
1181                 // convert the thing to our left to a scriptinset or create a new
1182                 // one if in the very first position of the array
1183                 if (cur.pos() == 0) {
1184                         lyxerr << "new scriptinset" << endl;
1185                         cur.insert(new MathScriptInset(up));
1186                 } else {
1187                         lyxerr << "converting prev atom " << endl;
1188                         cur.prevAtom() = MathAtom(new MathScriptInset(cur.prevAtom(), up));
1189                 }
1190                 lyxerr << "new scriptinset 2: cur:\n" << cur << endl;
1191                 --cur.pos();
1192                 lyxerr << "new scriptinset 3: cur:\n" << cur << endl;
1193                 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1194                 lyxerr << "new scriptinset 3: inset:\n" << inset << endl;
1195                 cur.push(*inset);
1196                 cur.idx() = 1;
1197                 cur.pos() = 0;
1198         }
1199         lyxerr << "pasting 1: safe:\n" << safe << endl;
1200         cur.paste(safe);
1201         cur.resetAnchor();
1202         lyxerr << "pasting 2: safe:\n" << safe << endl;
1203         return true;
1204 }