]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
Compile fix gcc 2.95 + stlport
[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_data.h"
17 #include "math_deliminset.h"
18 #include "math_factory.h"
19 #include "math_hullinset.h"
20 #include "math_mathmlstream.h"
21 #include "math_parser.h"
22 #include "math_spaceinset.h"
23 #include "math_support.h"
24 #include "math_mboxinset.h"
25
26 #include "BufferView.h"
27 #include "bufferview_funcs.h"
28 #include "cursor.h"
29 #include "debug.h"
30 #include "dispatchresult.h"
31 #include "funcrequest.h"
32 #include "gettext.h"
33 #include "LColor.h"
34 #include "undo.h"
35
36 #include "support/std_sstream.h"
37 #include "support/lstrings.h"
38
39 #include "frontends/Dialogs.h"
40 #include "frontends/LyXView.h"
41 #include "frontends/Painter.h"
42
43
44 using std::endl;
45 using std::string;
46 using std::istringstream;
47
48
49 MathNestInset::MathNestInset(idx_type nargs)
50         : cells_(nargs), lock_(false)
51 {}
52
53
54 MathInset::idx_type MathNestInset::nargs() const
55 {
56         return cells_.size();
57 }
58
59
60 MathArray & MathNestInset::cell(idx_type i)
61 {
62         return cells_[i];
63 }
64
65
66 MathArray const & MathNestInset::cell(idx_type i) const
67 {
68         return cells_[i];
69 }
70
71
72 void MathNestInset::getCursorPos(CursorSlice const & cur,
73         int & x, int & y) const
74 {
75         BOOST_ASSERT(cur.inset() == const_cast<MathNestInset *>(this));
76         MathArray const & ar = cur.cell();
77         x = ar.xo() + ar.pos2x(cur.pos());
78         y = ar.yo();
79         // move cursor visually into empty cells ("blue rectangles");
80         if (cur.cell().empty())
81                 x += 2;
82 }
83
84
85 void MathNestInset::substitute(MathMacro const & m)
86 {
87         for (idx_type i = 0; i < nargs(); ++i)
88                 cell(i).substitute(m);
89 }
90
91
92 void MathNestInset::metrics(MetricsInfo const & mi) const
93 {
94         MetricsInfo m = mi;
95         for (idx_type i = 0; i < nargs(); ++i)
96                 cell(i).metrics(m);
97 }
98
99
100 bool MathNestInset::idxNext(LCursor & cur) const
101 {
102         BOOST_ASSERT(cur.inset() == const_cast<MathNestInset *>(this));
103         if (cur.idx() == cur.lastidx())
104                 return false;
105         ++cur.idx();
106         cur.pos() = 0;
107         return true;
108 }
109
110
111 bool MathNestInset::idxRight(LCursor & cur) const
112 {
113         return idxNext(cur);
114 }
115
116
117 bool MathNestInset::idxPrev(LCursor & cur) const
118 {
119         BOOST_ASSERT(cur.inset() == const_cast<MathNestInset *>(this));
120         if (cur.idx() == 0)
121                 return false;
122         --cur.idx();
123         cur.pos() = cur.lastpos();
124         return true;
125 }
126
127
128 bool MathNestInset::idxLeft(LCursor & cur) const
129 {
130         return idxPrev(cur);
131 }
132
133
134 bool MathNestInset::idxFirst(LCursor & cur) const
135 {
136         BOOST_ASSERT(cur.inset() == const_cast<MathNestInset *>(this));
137         if (nargs() == 0)
138                 return false;
139         cur.idx() = 0;
140         cur.pos() = 0;
141         return true;
142 }
143
144
145 bool MathNestInset::idxLast(LCursor & cur) const
146 {
147         BOOST_ASSERT(cur.inset() == const_cast<MathNestInset *>(this));
148         if (nargs() == 0)
149                 return false;
150         cur.idx() = cur.lastidx();
151         cur.pos() = cur.lastpos();
152         return true;
153 }
154
155
156 bool MathNestInset::idxHome(LCursor & cur) const
157 {
158         BOOST_ASSERT(cur.inset() ==  const_cast<MathNestInset *>(this));
159         if (cur.pos() == 0)
160                 return false;
161         cur.pos() = 0;
162         return true;
163 }
164
165
166 bool MathNestInset::idxEnd(LCursor & cur) const
167 {
168         BOOST_ASSERT(cur.inset() ==  const_cast<MathNestInset *>(this));
169         if (cur.lastpos() == cur.lastpos())
170                 return false;
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 void MathNestInset::draw(PainterInfo &, int, int) const
190 {
191 #if 0
192         if (lock_)
193                 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
194                                         LColor::mathlockbg);
195 #endif
196 }
197
198
199 void MathNestInset::drawSelection(PainterInfo & pi, int x, int y) 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 (cur.inset() != const_cast<MathNestInset *>(this))
206                 return;
207         CursorSlice & s1 = cur.selBegin();
208         CursorSlice & s2 = cur.selEnd();
209         if (s1.idx() == s2.idx()) {
210                 MathArray const & c = s1.cell();
211                 lyxerr << "###### c.xo(): " << c.xo() << " c.yo(): " << c.yo() << endl;
212                 int x1 = c.xo() + c.pos2x(s1.pos());
213                 int y1 = c.yo() - c.ascent();
214                 int x2 = c.xo() + c.pos2x(s2.pos());
215                 int y2 = c.yo() + c.descent();
216                 //pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
217                 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::red);
218         } else {
219                 for (idx_type i = 0; i < nargs(); ++i) {
220                         if (idxBetween(i, s1.idx(), s2.idx())) {
221                                 MathArray const & c = cell(i);
222                                 int x1 = c.xo();
223                                 int y1 = c.yo() - c.ascent();
224                                 int x2 = c.xo() + c.width();
225                                 int y2 = c.yo() + c.descent();
226                                 //pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
227                                 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::red);
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 void MathNestInset::notifyCursorLeaves(idx_type idx)
308 {
309         cell(idx).notifyCursorLeaves();
310 }
311
312
313 void MathNestInset::handleFont
314         (LCursor & cur, string const & arg, string const & font)
315 {
316         // this whole function is a hack and won't work for incremental font
317         // changes...
318         //recordUndo(cur, Undo::ATOMIC);
319
320         if (cur.inset()->asMathInset()->name() == font)
321                 cur.handleFont(font);
322         else {
323                 cur.handleNest(createMathInset(font));
324                 cur.insert(arg);
325         }
326 }
327
328
329 void MathNestInset::handleFont2(LCursor & cur, string const & arg)
330 {
331         //recordUndo(cur, Undo::ATOMIC);
332         LyXFont font;
333         bool b;
334         bv_funcs::string2font(arg, font, b);
335         if (font.color() != LColor::inherit) {
336                 MathAtom at = createMathInset("color");
337                 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
338                 cur.handleNest(at, 1);
339         }
340 }
341
342
343 DispatchResult
344 MathNestInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
345 {
346         lyxerr << "*** MathNestInset: request: " << cmd << std::endl;
347         //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
348         //      << " arg: '" << cmd.argument
349         //      << "' x: '" << cmd.x
350         //      << " y: '" << cmd.y
351         //      << "' button: " << cmd.button() << endl;
352
353         switch (cmd.action) {
354
355         case LFUN_PASTE:
356                 if (!cmd.argument.empty()) {
357                         MathArray ar;
358                         mathed_parse_cell(ar, cmd.argument);
359                         cur.cell().insert(cur.pos(), ar);
360                         cur.pos() += ar.size();
361                 }
362                 return DispatchResult(true, true);
363 /*
364         case LFUN_PASTE: {
365                 size_t n = 0;
366                 istringstream is(cmd.argument.c_str());
367                 is >> n;
368                 if (was_macro)
369                         cur.macroModeClose();
370                 //recordUndo(cur, Undo::ATOMIC);
371                 cur.selPaste(n);
372                 return DispatchResult(true, true);
373         }
374 */
375
376         case LFUN_PASTESELECTION:
377                 return dispatch(cur, FuncRequest(LFUN_PASTE, cur.bv().getClipboard())); 
378
379         case LFUN_MOUSE_PRESS:
380                 if (cmd.button() == mouse_button::button2)
381                         return priv_dispatch(cur, FuncRequest(LFUN_PASTESELECTION));
382                 return DispatchResult(false);
383
384         case LFUN_RIGHTSEL:
385                 cur.selection() = true; // fall through...
386         case LFUN_RIGHT:
387                 return cur.right() ?
388                         DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
389                 //lyxerr << "calling scroll 20" << endl;
390                 //scroll(&cur.bv(), 20);
391                 // write something to the minibuffer
392                 //cur.bv().owner()->message(cur.info());
393
394         case LFUN_LEFTSEL:
395                 cur.selection() = true; // fall through
396         case LFUN_LEFT:
397                 return cur.left() ?
398                         DispatchResult(true, true) : DispatchResult(false, FINISHED);
399
400         case LFUN_UPSEL:
401                 cur.selection() = true; // fall through
402         case LFUN_UP:
403                 return cur.up() ?
404                         DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
405
406         case LFUN_DOWNSEL:
407                 cur.selection() = true; // fall through
408         case LFUN_DOWN:
409                 return cur.down() ?
410                         DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
411
412         case LFUN_WORDSEL:
413                 cur.home();
414                 cur.selection() = true;
415                 cur.end();
416                 return DispatchResult(true, true);
417
418         case LFUN_UP_PARAGRAPHSEL:
419         case LFUN_UP_PARAGRAPH:
420         case LFUN_DOWN_PARAGRAPHSEL:
421         case LFUN_DOWN_PARAGRAPH:
422                 return DispatchResult(true, FINISHED);
423
424         case LFUN_HOMESEL:
425         case LFUN_WORDLEFTSEL:
426                 cur.selection() = true; // fall through
427         case LFUN_HOME:
428         case LFUN_WORDLEFT:
429                 return cur.home()
430                         ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
431
432         case LFUN_ENDSEL:
433         case LFUN_WORDRIGHTSEL:
434                 cur.selection() = true; // fall through
435         case LFUN_END:
436         case LFUN_WORDRIGHT:
437                 return cur.end()
438                         ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
439
440         case LFUN_PRIORSEL:
441         case LFUN_PRIOR:
442         case LFUN_BEGINNINGBUFSEL:
443         case LFUN_BEGINNINGBUF:
444                 return DispatchResult(true, FINISHED);
445
446         case LFUN_NEXTSEL:
447         case LFUN_NEXT:
448         case LFUN_ENDBUFSEL:
449         case LFUN_ENDBUF:
450                 return DispatchResult(false, FINISHED_RIGHT);
451
452         case LFUN_CELL_FORWARD:
453                 cur.inset()->idxNext(cur);
454                 return DispatchResult(true, true);
455
456         case LFUN_CELL_BACKWARD:
457                 cur.inset()->idxPrev(cur);
458                 return DispatchResult(true, true);
459
460         case LFUN_DELETE_WORD_BACKWARD:
461         case LFUN_BACKSPACE:
462                 //recordUndo(cur, Undo::ATOMIC);
463                 cur.backspace();
464                 return DispatchResult(true, true);
465
466         case LFUN_DELETE_WORD_FORWARD:
467         case LFUN_DELETE:
468                 //recordUndo(cur, Undo::ATOMIC);
469                 cur.erase();
470                 return DispatchResult(true, FINISHED);
471
472         case LFUN_ESCAPE:
473                 if (!cur.selection())
474                         return DispatchResult(true, true);
475                 cur.selClear();
476                 return DispatchResult(false);
477
478         case LFUN_INSET_TOGGLE:
479                 cur.lockToggle();
480                 return DispatchResult(true, true);
481
482         case LFUN_SELFINSERT:
483                 if (!cmd.argument.empty()) {
484                         //recordUndo(cur, Undo::ATOMIC);
485                         if (cmd.argument.size() == 1) {
486                                 if (cur.interpret(cmd.argument[0]))
487                                         return DispatchResult(true, true);
488                                 else
489                                         return DispatchResult(false, FINISHED_RIGHT);
490                         }
491                         cur.insert(cmd.argument);
492                 }
493                 return DispatchResult(false, FINISHED_RIGHT);
494
495
496 #if 0
497 //
498 // this needs to be incorporated
499 //
500         //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
501         //      << " arg: '" << cmd.argument
502         //      << "' x: '" << cmd.x
503         //      << " y: '" << cmd.y
504         //      << "' button: " << cmd.button() << endl;
505
506         // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
507         bool remove_inset = false;
508
509         switch (cmd.action) {
510                 case LFUN_MOUSE_PRESS:
511                         //lyxerr << "Mouse single press" << endl;
512                         return lfunMousePress(cur, cmd);
513                 case LFUN_MOUSE_MOTION:
514                         //lyxerr << "Mouse motion" << endl;
515                         return lfunMouseMotion(cur, cmd);
516                 case LFUN_MOUSE_RELEASE:
517                         //lyxerr << "Mouse single release" << endl;
518                         return lfunMouseRelease(cur, cmd);
519                 case LFUN_MOUSE_DOUBLE:
520                         //lyxerr << "Mouse double" << endl;
521                         return dispatch(cur, FuncRequest(LFUN_WORDSEL));
522                 default:
523                         break;
524         }
525
526         DispatchResult result(true);
527         bool was_macro     = cur.inMacroMode();
528
529         cur.normalize();
530         cur.touch();
531
532         switch (cmd.action) {
533
534         case LFUN_MATH_LIMITS:
535                 //recordUndo(cur, Undo::ATOMIC);
536                 cur.dispatch(cmd);
537                 break;
538 #endif
539
540         //    case LFUN_GETXY:
541         //      sprintf(dispatch_buffer, "%d %d",);
542         //      DispatchResult= dispatch_buffer;
543         //      break;
544         case LFUN_SETXY: {
545                 lyxerr << "LFUN_SETXY broken!" << endl;
546                 int x = 0;
547                 int y = 0;
548                 istringstream is(cmd.argument.c_str());
549                 is >> x >> y;
550                 cur.setScreenPos(x, y);
551                 return DispatchResult(true, true);
552         }
553
554         case LFUN_CUT:
555                 //recordUndo(cur, Undo::DELETE);
556                 cur.selCut();
557                 return DispatchResult(true, true);
558
559         case LFUN_COPY:
560                 cur.selCopy();
561                 return DispatchResult(true, true);
562
563
564         // Special casing for superscript in case of LyX handling
565         // dead-keys:
566         case LFUN_CIRCUMFLEX:
567                 if (cmd.argument.empty()) {
568                         // do superscript if LyX handles
569                         // deadkeys
570                         //recordUndo(cur, Undo::ATOMIC);
571                         cur.script(true);
572                 }
573                 return DispatchResult(true, true);
574
575         case LFUN_UMLAUT:
576         case LFUN_ACUTE:
577         case LFUN_GRAVE:
578         case LFUN_BREVE:
579         case LFUN_DOT:
580         case LFUN_MACRON:
581         case LFUN_CARON:
582         case LFUN_TILDE:
583         case LFUN_CEDILLA:
584         case LFUN_CIRCLE:
585         case LFUN_UNDERDOT:
586         case LFUN_TIE:
587         case LFUN_OGONEK:
588         case LFUN_HUNG_UMLAUT:
589                 return DispatchResult(true, true);
590
591         //  Math fonts
592         case LFUN_FREEFONT_APPLY:
593         case LFUN_FREEFONT_UPDATE:
594                 handleFont2(cur, cmd.argument);
595                 return DispatchResult(true, true);
596
597         case LFUN_BOLD:
598                 handleFont(cur, cmd.argument, "mathbf");
599                 return DispatchResult(true, true);
600         case LFUN_SANS:
601                 handleFont(cur, cmd.argument, "mathsf");
602                 return DispatchResult(true, true);
603         case LFUN_EMPH:
604                 handleFont(cur, cmd.argument, "mathcal");
605                 return DispatchResult(true, true);
606         case LFUN_ROMAN:
607                 handleFont(cur, cmd.argument, "mathrm");
608                 return DispatchResult(true, true);
609         case LFUN_CODE:
610                 handleFont(cur, cmd.argument, "texttt");
611                 return DispatchResult(true, true);
612         case LFUN_FRAK:
613                 handleFont(cur, cmd.argument, "mathfrak");
614                 return DispatchResult(true, true);
615         case LFUN_ITAL:
616                 handleFont(cur, cmd.argument, "mathit");
617                 return DispatchResult(true, true);
618         case LFUN_NOUN:
619                 handleFont(cur, cmd.argument, "mathbb");
620                 return DispatchResult(true, true);
621         //case LFUN_FREEFONT_APPLY:
622                 handleFont(cur, cmd.argument, "textrm");
623                 return DispatchResult(true, true);
624         case LFUN_DEFAULT:
625                 handleFont(cur, cmd.argument, "textnormal");
626                 return DispatchResult(true, true);
627
628         case LFUN_MATH_MODE:
629 #if 1
630                 cur.macroModeClose();
631                 cur.selClearOrDel();
632                 cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
633                 cur.posLeft();
634                 cur.pushLeft(cur.nextInset());
635 #else
636                 if (cur.currentMode() == InsetBase::TEXT_MODE)
637                         cur.niceInsert(MathAtom(new MathHullInset("simple")));
638                 else
639                         handleFont(cur, cmd.argument, "textrm");
640                 //cur.owner()->message(_("math text mode toggled"));
641 #endif
642                 return DispatchResult(true, true);
643
644         case LFUN_MATH_SIZE:
645 #if 0
646                 if (!arg.empty()) {
647                         //recordUndo(cur, Undo::ATOMIC);
648                         cur.setSize(arg);
649                 }
650 #endif
651                 return DispatchResult(true, true);
652
653         case LFUN_INSERT_MATRIX: {
654                 //recordUndo(cur, Undo::ATOMIC);
655                 unsigned int m = 1;
656                 unsigned int n = 1;
657                 string v_align;
658                 string h_align;
659                 istringstream is(cmd.argument);
660                 is >> m >> n >> v_align >> h_align;
661                 if (m < 1)
662                         m = 1;
663                 if (n < 1)
664                         n = 1;
665                 v_align += 'c';
666                 cur.niceInsert(
667                         MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
668                 return DispatchResult(true, true);
669         }
670
671         case LFUN_MATH_DELIM: {
672                 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
673                 string ls;
674                 string rs = lyx::support::split(cmd.argument, ls, ' ');
675                 // Reasonable default values
676                 if (ls.empty())
677                         ls = '(';
678                 if (rs.empty())
679                         rs = ')';
680                 //recordUndo(cur, Undo::ATOMIC);
681                 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
682                 return DispatchResult(true, true);
683         }
684
685         case LFUN_SPACE_INSERT:
686         case LFUN_MATH_SPACE:
687                 //recordUndo(cur, Undo::ATOMIC);
688                 cur.insert(MathAtom(new MathSpaceInset(",")));
689                 return DispatchResult(true, true);
690
691         case LFUN_UNDO:
692 #warning look here
693                 //cur.bv().owner()->message(_("Invalid action in math mode!"));
694                 return DispatchResult(true, true);
695
696         case LFUN_INSET_ERT:
697                 // interpret this as if a backslash was typed
698                 //recordUndo(cur, Undo::ATOMIC);
699                 cur.interpret('\\');
700                 return DispatchResult(true, true);
701
702 #if 0
703         case LFUN_BREAKPARAGRAPH:
704         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
705         case LFUN_BREAKPARAGRAPH_SKIP:
706                 cmd.argument = "\n";
707                 //recordUndo(cur, Undo::ATOMIC);
708                 cur.niceInsert(argument);
709                 return DispatchResult(true, true);
710 #endif
711
712 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
713 // handling such that "self-insert" works on "arbitrary stuff" too, and
714 // math-insert only handles special math things like "matrix".
715         case LFUN_INSERT_MATH:
716                 //recordUndo(cur, Undo::ATOMIC);
717                 cur.niceInsert(cmd.argument);
718                 return DispatchResult(true, true);
719
720         case LFUN_DIALOG_SHOW:
721                 return DispatchResult(false);
722
723         case LFUN_DIALOG_SHOW_NEW_INSET: {
724                 string const & name = cmd.argument;
725                 string data;
726 #if 0
727                 if (name == "ref") {
728                         RefInset tmp(name);
729                         data = tmp.createDialogStr(name);
730                 }
731 #endif
732                 if (data.empty())
733                         return DispatchResult(false);
734                 cur.bv().owner()->getDialogs().show(name, data, 0);
735                 return DispatchResult(true, true);
736         }
737
738         case LFUN_INSET_APPLY: {
739                 string const name = cmd.getArg(0);
740                 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
741
742                 if (base) {
743                         FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
744                         return base->dispatch(cur, fr);
745                 }
746                 MathArray ar;
747                 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
748                         cur.insert(ar);
749                         return DispatchResult(true, true);
750                 }
751                 return DispatchResult(false);
752         }
753
754 #warning look here
755 #if 0
756
757         case LFUN_WORD_REPLACE:
758         case LFUN_WORD_FIND:
759                 return
760                         searchForward(&cur.bv(), cmd.getArg(0), false, false)
761                                 ? DispatchResult(true, true) : DispatchResult(false);
762
763         case LFUN_INSERT_MATH:
764         case LFUN_INSERT_MATRIX:
765         case LFUN_MATH_DELIM: {
766                 MathHullInset * f = new MathHullInset;
767                 if (openNewInset(cur, f)) {
768                         cur.inset()->dispatch(cur, FuncRequest(LFUN_MATH_MUTATE, "simple"));
769                         cur.inset()->dispatch(cur, cmd);
770                 }
771                 return DispatchResult(true, true);
772         }
773
774         cur.normalize();
775         cur.touch();
776
777         BOOST_ASSERT(cur.inMathed());
778
779         if (result.dispatched()) {
780                 revealCodes(cur);
781                 cur.bv().stuffClipboard(cur.grabSelection());
782         } else {
783                 cur.releaseMathCursor();
784                 if (remove_inset)
785                         cur.bv().owner()->dispatch(FuncRequest(LFUN_DELETE));
786         }
787
788         return result;  // original version
789 #endif
790
791         default:
792                 return MathDimInset::priv_dispatch(cur, cmd);
793         }
794 }
795
796
797 void MathNestInset::edit(LCursor & cur, int x, int y)
798 {
799         lyxerr << "Called MathNestInset::edit with '" << x << ' ' << y << "'" << endl;
800         cur.push(this);
801         int idx_min = 0;
802         int dist_min = 1000000;
803         for (idx_type i = 0; i < nargs(); ++i) {
804                 int d = cell(i).dist(x, y);
805                 if (d < dist_min) {
806                         dist_min = d;
807                         idx_min = i;
808                 }
809         }
810         MathArray & ar = cell(idx_min);
811         cur.push(this);
812         cur.idx() = idx_min;
813         cur.pos() = ar.x2pos(x - ar.xo());
814         lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
815         if (dist_min == 0) {
816                 // hit inside cell
817                 for (pos_type i = 0, n = ar.size(); i < n; ++i)
818                         if (ar[i]->covers(x, y))
819                                 ar[i].nucleus()->edit(cur, x, y);
820         }
821 }