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