]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
more lfun localization
[lyx.git] / src / mathed / formulabase.C
1 /*
2 *  File:        formulabase.C
3 *  Purpose:     Implementation of common parts of the LyX  math insets
4 *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 *  Created:     January 1996
6 *
7 *  Copyright: 1996-1998 Alejandro Aguilar Sierra
8 *
9 *  Version: 0.4, Lyx project.
10 *
11 *   You are free to use and modify this code under the terms of
12 *   the GNU General Public Licence version 2 or later.
13 */
14
15 #include <config.h>
16 #include <fstream>
17
18 #include "Lsstream.h"
19 #include "support/LAssert.h"
20
21 #ifdef __GNUG__
22 #pragma implementation
23 #endif
24
25 #include "formula.h"
26 #include "formulamacro.h"
27 #include "lyxrc.h"
28 #include "funcrequest.h"
29 #include "BufferView.h"
30 #include "lyxtext.h"
31 #include "lyxfunc.h"
32 #include "gettext.h"
33 #include "LaTeXFeatures.h"
34 #include "debug.h"
35 #include "math_support.h"
36 #include "math_metricsinfo.h"
37 #include "support/lstrings.h"
38 #include "frontends/LyXView.h"
39 #include "frontends/font_metrics.h"
40 #include "frontends/mouse_state.h"
41 #include "Lsstream.h"
42 #include "math_arrayinset.h"
43 #include "math_charinset.h"
44 #include "math_deliminset.h"
45 #include "math_cursor.h"
46 #include "math_factory.h"
47 #include "math_fontinset.h"
48 #include "math_hullinset.h"
49 #include "math_iterator.h"
50 #include "math_macrotable.h"
51 #include "math_parser.h"
52 #include "math_pos.h"
53 #include "math_spaceinset.h"
54 #include "undo_funcs.h"
55 #include "textpainter.h"
56 #include "frontends/Dialogs.h"
57 #include "intl.h"
58 #include "insets/insetcommandparams.h"
59 #include "ref_inset.h"
60
61 using std::endl;
62 using std::ostream;
63 using std::vector;
64 using std::abs;
65
66 MathCursor * mathcursor = 0;
67
68
69 namespace {
70
71 // local global
72 int first_x;
73 int first_y;
74
75
76
77 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
78 {
79         if (!bv->insertInset(new_inset)) {
80                 delete new_inset;
81                 return false;
82         }
83         new_inset->edit(bv, true);
84         return true;
85 }
86
87
88 } // namespace anon
89
90
91
92 InsetFormulaBase::InsetFormulaBase()
93         : font_(), xo_(0), yo_(0)
94 {
95         // This is needed as long the math parser is not re-entrant
96         initMath();
97         //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
98         //lyxerr << "sizeof MathMetricsInfo: " << sizeof(MathMetricsInfo) << "\n";
99         //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
100         //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
101 }
102
103
104 // simply scrap this function if you want
105 void InsetFormulaBase::mutateToText()
106 {
107 #if 0
108         // translate to latex
109         ostringstream os;
110         latex(NULL, os, false, false);
111         string str = os.str();
112
113         // insert this text
114         LyXText * lt = view_->getLyXText();
115         string::const_iterator cit = str.begin();
116         string::const_iterator end = str.end();
117         for (; cit != end; ++cit)
118                 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
119
120         // remove ourselves
121         //view_->owner()->dispatch(LFUN_ESCAPE);
122 #endif
123 }
124
125
126 void InsetFormulaBase::handleFont
127         (BufferView * bv, string const & arg, string const & font)
128 {
129         bv->lockedInsetStoreUndo(Undo::EDIT);
130         bool sel = mathcursor->selection();
131         if (sel)
132                 updateLocal(bv, true);
133         mathcursor->handleNest(createMathInset(font));
134         mathcursor->insert(arg);
135         if (!sel)
136                 updateLocal(bv, false);
137 }
138
139
140 // Check if uses AMS macros
141 void InsetFormulaBase::validate(LaTeXFeatures &) const
142 {}
143
144
145 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
146 {
147         font_ = f;
148         metrics(bv);
149 }
150
151
152 void InsetFormulaBase::metrics(BufferView * bv) const
153 {
154         if (bv)
155                 view_ = bv->owner()->view();
156         MathMetricsInfo mi;
157         mi.base.style = LM_ST_TEXT;
158         mi.base.font  = font_;
159         mi.base.font.setColor(LColor::math);
160         par()->metrics(mi);
161 }
162
163
164 string const InsetFormulaBase::editMessage() const
165 {
166         return _("Math editor mode");
167 }
168
169
170 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
171 {
172         if (!bv->lockInset(this))
173                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
174         releaseMathCursor(bv);
175         mathcursor = new MathCursor(this, true);
176         metrics(bv);
177         mathcursor->setPos(x, y);
178         //lyxerr << "setting pos to " << x << "," << y << "\n";
179
180         // if that is removed, we won't get the magenta box when entering an
181         // inset for the first time
182         bv->updateInset(this, false);
183 }
184
185
186 void InsetFormulaBase::edit(BufferView * bv, bool front)
187 {
188         if (!bv->lockInset(this))
189                 lyxerr << "Cannot lock math inset in edit call!\n";
190         releaseMathCursor(bv);
191         mathcursor = new MathCursor(this, front);
192         metrics(bv);
193         bv->updateInset(this, false);
194 }
195
196
197 void InsetFormulaBase::insetUnlock(BufferView * bv)
198 {
199         if (mathcursor) {
200                 if (mathcursor->inMacroMode()) {
201                         mathcursor->macroModeClose();
202                         updateLocal(bv, true);
203                 }
204                 releaseMathCursor(bv);
205         }
206         generatePreview();
207         bv->updateInset(this, false);
208 }
209
210
211 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
212 {
213         // calling metrics here destroys the cached xo,yo positions e.g. in
214         // MathParboxinset. And it would be too expensive anyway...
215         //metrics(bv);
216         mathcursor->getPos(x, y);
217         //x -= xo_;
218         y -= yo_;
219         //lyxerr << "getCursorPos: " << x << " " << y << "\n";
220 }
221
222
223 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
224 {
225         //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
226         if (isCursorVisible())
227                 hideInsetCursor(bv);
228         else
229                 showInsetCursor(bv);
230 }
231
232
233 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
234 {
235         if (isCursorVisible())
236                 return;
237         fitInsetCursor(bv);
238         int x, y, asc, des;
239         getCursorPos(bv, x, y);
240         math_font_max_dim(font_, asc, des);
241         bv->showLockedInsetCursor(x, y, asc, des);
242         setCursorVisible(true);
243         //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
244 }
245
246
247 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
248 {
249         if (!isCursorVisible())
250                 return;
251         bv->hideLockedInsetCursor();
252         setCursorVisible(false);
253         //lyxerr << "hideInsetCursor: \n";
254 }
255
256
257 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
258 {
259         if (!mathcursor)
260                 return;
261         int x, y, asc, des;
262         math_font_max_dim(font_, asc, des);
263         getCursorPos(bv, x, y);
264         //y += yo_;
265         //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
266         bv->fitLockedInsetCursor(x, y, asc, des);
267 }
268
269
270 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
271 {
272         if (mathcursor)
273                 bv->updateInset(this, false);
274 }
275
276
277 vector<string> const InsetFormulaBase::getLabelList() const
278 {
279   return vector<string>();
280 }
281
282
283 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
284 {
285         bv->updateInset(this, dirty);
286 }
287
288
289 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
290         int x, int y, mouse_button::state button)
291 {
292         if (!mathcursor)
293                 return false;
294
295         //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
296         hideInsetCursor(bv);
297         showInsetCursor(bv);
298         bv->updateInset(this, false);
299
300         if (button == mouse_button::button3) {
301                 // try to dispatch to enclosed insets first
302                 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_RELEASE, x, y, 3)))
303                         return true;
304
305                 // launch math panel for right mouse button
306                 bv->owner()->getDialogs().showMathPanel();
307                 return true;
308         }
309
310         if (button == mouse_button::button1) {
311                 // try to dispatch to enclosed insets first
312                 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_RELEASE, x, y, 1)))
313                         return true;
314
315                 // try to set the cursor
316                 //delete mathcursor;
317                 //mathcursor = new MathCursor(this, x == 0);
318                 //metrics(bv);
319                 //mathcursor->setPos(x + xo_, y + yo_);
320                 return true;
321         }
322         return false;
323 }
324
325
326 void InsetFormulaBase::insetButtonPress(BufferView * bv,
327                                         int x, int y, mouse_button::state button)
328 {
329         //lyxerr << "insetButtonPress: "
330         //      << x << " " << y << " but: " << button << "\n";
331         //lyxerr << "formula: ";
332         //par()->dump();
333
334         releaseMathCursor(bv);
335         mathcursor = new MathCursor(this, x == 0);
336
337         if (button == mouse_button::button1) {
338                 // just set the cursor here
339                 //lyxerr << "setting cursor\n";
340                 metrics(bv);
341                 first_x = x;
342                 first_y = y;
343                 mathcursor->selClear();
344                 mathcursor->setPos(x + xo_, y + yo_);
345
346                 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_PRESS, x, y, 1))) {
347                         //delete mathcursor;
348                         return;
349                 }
350
351         }
352         if (button == mouse_button::button3) {
353                 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_PRESS, x, y, 3))) {
354                         //delete mathcursor;
355                         return;
356                 }
357         }
358         bv->updateInset(this, false);
359 }
360
361
362 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
363         int x, int y, mouse_button::state button)
364 {
365         if (!mathcursor)
366                 return;
367
368         if (button == mouse_button::button1)
369                 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_MOTION, x, y, 1)))
370                         return;
371
372         if (button == mouse_button::button3)
373                 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_MOTION, x, y, 3)))
374                         return;
375
376         if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
377                 //lyxerr << "insetMotionNotify: ignored\n";
378                 return;
379         }
380         first_x = x;
381         first_y = y;
382
383         if (!mathcursor->selection())
384                 mathcursor->selStart();
385
386         //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
387         //      << ' ' << button << "\n";
388         hideInsetCursor(bv);
389         mathcursor->setPos(x + xo_, y + yo_);
390         showInsetCursor(bv);
391         bv->updateInset(this, false);
392 }
393
394
395 UpdatableInset::RESULT
396 InsetFormulaBase::localDispatch(FuncRequest const & ev)
397 {
398         //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
399         //      << " arg: '" << arg
400         //      << "' cursor: " << mathcursor << "\n";
401
402         if (!mathcursor)
403                 return UNDISPATCHED;
404
405         BufferView * bv    = ev.view();
406         string argument    = ev.argument;
407         RESULT result      = DISPATCHED;
408         bool sel           = false;
409         bool was_macro     = mathcursor->inMacroMode();
410         bool was_selection = mathcursor->selection();
411
412         hideInsetCursor(bv);
413
414         mathcursor->normalize();
415         mathcursor->touch();
416
417         switch (ev.action) {
418
419         case LFUN_MATH_HALIGN:
420         case LFUN_MATH_VALIGN:
421         case LFUN_MATH_ROW_INSERT:
422         case LFUN_MATH_ROW_DELETE:
423         case LFUN_MATH_COLUMN_INSERT:
424         case LFUN_MATH_COLUMN_DELETE:
425         case LFUN_MATH_NUMBER:
426         case LFUN_MATH_NONUMBER:
427         case LFUN_TABINSERT:
428         case LFUN_BREAKLINE:
429         case LFUN_DELETE_LINE_FORWARD:
430         case LFUN_INSERT_LABEL:
431                 bv->lockedInsetStoreUndo(Undo::EDIT);
432                 mathcursor->dispatch(ev);
433                 updateLocal(bv, true);
434                 break;
435
436         case LFUN_WORDRIGHTSEL:
437         case LFUN_RIGHTSEL:
438                 sel = true; // fall through...
439         case LFUN_RIGHT:
440                 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
441                 //lyxerr << "calling scroll 20\n";
442                 //scroll(bv, 20);
443                 updateLocal(bv, false);
444                 // write something to the minibuffer
445                 //bv->owner()->message(mathcursor->info());
446                 break;
447
448         case LFUN_WORDLEFTSEL:
449         case LFUN_LEFTSEL:
450                 sel = true; // fall through
451         case LFUN_LEFT:
452                 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
453                 updateLocal(bv, false);
454                 break;
455
456         case LFUN_UPSEL:
457                 sel = true; // fall through
458         case LFUN_UP:
459                 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
460                 updateLocal(bv, false);
461                 break;
462
463         case LFUN_DOWNSEL:
464                 sel = true; // fall through
465         case LFUN_DOWN:
466                 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
467                 updateLocal(bv, false);
468                 break;
469
470         case LFUN_HOMESEL:
471                 sel = true; // fall through
472         case LFUN_HOME:
473                 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
474                 updateLocal(bv, false);
475                 break;
476
477         case LFUN_ENDSEL:
478                 sel = true; // fall through
479         case LFUN_END:
480                 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
481                 updateLocal(bv, false);
482                 break;
483
484         case LFUN_PRIORSEL:
485         case LFUN_PRIOR:
486                 result = FINISHED_UP;
487                 updateLocal(bv, false);
488                 break;
489
490         case LFUN_NEXTSEL:
491         case LFUN_NEXT:
492                 result = FINISHED_DOWN;
493                 updateLocal(bv, false);
494                 break;
495
496         case LFUN_TAB:
497                 mathcursor->idxNext();
498                 updateLocal(bv, false);
499                 break;
500
501         case LFUN_SHIFT_TAB:
502                 mathcursor->idxPrev();
503                 updateLocal(bv, false);
504                 break;
505
506         case LFUN_DELETE_WORD_BACKWARD:
507         case LFUN_BACKSPACE:
508                 bv->lockedInsetStoreUndo(Undo::DELETE);
509                 mathcursor->backspace();
510                 updateLocal(bv, true);
511                 break;
512
513         case LFUN_DELETE_WORD_FORWARD:
514         case LFUN_DELETE:
515                 bv->lockedInsetStoreUndo(Undo::DELETE);
516                 mathcursor->erase();
517                 bv->updateInset(this, true);
518                 break;
519
520         //    case LFUN_GETXY:
521         //      sprintf(dispatch_buffer, "%d %d",);
522         //      dispatch_result = dispatch_buffer;
523         //      break;
524         case LFUN_SETXY: {
525                 lyxerr << "LFUN_SETXY broken!\n";
526                 int x = 0;
527                 int y = 0;
528                 istringstream is(ev.argument.c_str());
529                 is >> x >> y;
530                 mathcursor->setPos(x, y);
531                 updateLocal(bv, false);
532                 break;
533         }
534
535         case LFUN_PASTE:
536                 if (was_macro)
537                         mathcursor->macroModeClose();
538                 bv->lockedInsetStoreUndo(Undo::EDIT);
539                 mathcursor->selPaste();
540                 updateLocal(bv, true);
541                 break;
542
543         case LFUN_CUT:
544                 bv->lockedInsetStoreUndo(Undo::DELETE);
545                 mathcursor->selCut();
546                 updateLocal(bv, true);
547                 break;
548
549         case LFUN_COPY:
550                 mathcursor->selCopy();
551                 break;
552
553
554         // Special casing for superscript in case of LyX handling
555         // dead-keys:
556         case LFUN_CIRCUMFLEX:
557                 if (ev.argument.empty()) {
558                         // do superscript if LyX handles
559                         // deadkeys
560                         bv->lockedInsetStoreUndo(Undo::EDIT);
561                         mathcursor->script(true);
562                         updateLocal(bv, true);
563                 }
564                 break;
565
566         case LFUN_UMLAUT:
567         case LFUN_ACUTE:
568         case LFUN_GRAVE:
569         case LFUN_BREVE:
570         case LFUN_DOT:
571         case LFUN_MACRON:
572         case LFUN_CARON:
573         case LFUN_TILDE:
574         case LFUN_CEDILLA:
575         case LFUN_CIRCLE:
576         case LFUN_UNDERDOT:
577         case LFUN_TIE:
578         case LFUN_OGONEK:
579         case LFUN_HUNG_UMLAUT:
580                 break;
581
582         //  Math fonts
583         case LFUN_GREEK_TOGGLE: handleFont(bv, ev.argument, "lyxgreek"); break;
584         case LFUN_BOLD:         handleFont(bv, ev.argument, "textbf"); break;
585         case LFUN_SANS:         handleFont(bv, ev.argument, "textsf"); break;
586         case LFUN_EMPH:         handleFont(bv, ev.argument, "mathcal"); break;
587         case LFUN_ROMAN:        handleFont(bv, ev.argument, "mathrm"); break;
588         case LFUN_CODE:         handleFont(bv, ev.argument, "texttt"); break;
589         case LFUN_FRAK:         handleFont(bv, ev.argument, "mathfrak"); break;
590         case LFUN_ITAL:         handleFont(bv, ev.argument, "mathit"); break;
591         case LFUN_NOUN:         handleFont(bv, ev.argument, "mathbb"); break;
592         case LFUN_DEFAULT:      handleFont(bv, ev.argument, "textnormal"); break;
593         case LFUN_FREE:         handleFont(bv, ev.argument, "textrm"); break;
594
595         case LFUN_GREEK:
596                 handleFont(bv, ev.argument, "lyxgreek1");
597                 if (ev.argument.size())
598                         mathcursor->interpret(ev.argument);
599                 break;
600
601         case LFUN_MATH_MODE:
602                 if (mathcursor->currentMode()) {
603                         handleFont(bv, ev.argument, "textrm");
604                 } else {
605                         mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
606                         updateLocal(bv, true);
607                 }
608                 //bv->owner()->message(_("math text mode toggled"));
609                 break;
610
611         case LFUN_MATH_LIMITS:
612                 bv->lockedInsetStoreUndo(Undo::EDIT);
613                 if (mathcursor->toggleLimits())
614                         updateLocal(bv, true);
615                 break;
616
617         case LFUN_MATH_SIZE:
618 #if 0
619                 if (!arg.empty()) {
620                         bv->lockedInsetStoreUndo(Undo::EDIT);
621                         mathcursor->setSize(arg);
622                         updateLocal(bv, true);
623                 }
624 #endif
625                 break;
626
627         case LFUN_INSERT_MATRIX:
628                 if (!ev.argument.empty()) {
629                         bv->lockedInsetStoreUndo(Undo::EDIT);
630                         mathcursor->interpret("matrix " + ev.argument);
631                         updateLocal(bv, true);
632                 }
633                 break;
634
635         case LFUN_SUPERSCRIPT:
636         case LFUN_SUBSCRIPT:
637         {
638                 bv->lockedInsetStoreUndo(Undo::EDIT);
639                 mathcursor->script(ev.action == LFUN_SUPERSCRIPT);
640                 updateLocal(bv, true);
641                 break;
642         }
643
644         case LFUN_MATH_DELIM:
645         {
646                 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
647                 string ls;
648                 string rs = split(ev.argument, ls, ' ');
649                 // Reasonable default values
650                 if (ls.empty())
651                         ls = '(';
652                 if (rs.empty())
653                         rs = ')';
654
655                 bv->lockedInsetStoreUndo(Undo::EDIT);
656                 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
657                 updateLocal(bv, true);
658                 break;
659         }
660
661         case LFUN_PROTECTEDSPACE:
662         case LFUN_MATH_SPACE:
663                 bv->lockedInsetStoreUndo(Undo::EDIT);
664                 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
665                 updateLocal(bv, true);
666                 break;
667
668         case LFUN_UNDO:
669                 bv->owner()->message(_("Invalid action in math mode!"));
670                 break;
671
672
673         case LFUN_EXEC_COMMAND:
674                 result = UNDISPATCHED;
675                 break;
676
677         case LFUN_INSET_ERT:
678                 // interpret this as if a backslash was typed
679                 bv->lockedInsetStoreUndo(Undo::EDIT);
680                 mathcursor->interpret('\\');
681                 updateLocal(bv, true);
682                 break;
683
684         case LFUN_BREAKPARAGRAPH:
685         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
686         case LFUN_BREAKPARAGRAPH_SKIP:
687                 argument = "\n";
688                 // fall through
689
690         case -1:
691         case LFUN_INSERT_MATH:
692         case LFUN_SELFINSERT:
693                 if (!argument.empty()) {
694                         bv->lockedInsetStoreUndo(Undo::EDIT);
695                         if (argument.size() == 1)
696                                 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
697                         else
698                                 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
699                         updateLocal(bv, true);
700                 }
701                 break;
702
703         case LFUN_MATH_PANEL:
704                 result = UNDISPATCHED;
705                 break;
706
707         case LFUN_ESCAPE:
708                 if (mathcursor->selection())
709                         mathcursor->selClear();
710                 else
711                         result = UNDISPATCHED;
712                 break;
713
714         case LFUN_INSET_TOGGLE:
715                 mathcursor->insetToggle();
716                 updateLocal(bv, true);
717                 break;
718
719         case LFUN_REF_INSERT:
720                 //if (argument.empty()) {
721                 //      InsetCommandParams p("ref");
722                 //      owner_->getDialogs().createRef(p.getAsString());
723                 //} else {
724                 //      InsetCommandParams p;
725                 //      p.setFromString(argument);
726
727                 //      InsetRef * inset = new InsetRef(p, *buffer_);
728                 //      if (!insertInset(inset))
729                 //              delete inset;
730                 //      else
731                 //              updateInset(inset, true);
732                 //}
733                 //
734                 if (ev.argument.empty()) {
735                         InsetCommandParams p("ref");
736                         bv->owner()->getDialogs().createRef(p.getAsString());
737                 } else {
738                         //mathcursor->handleNest(new InsetRef2);
739                         //mathcursor->insert(arg);
740                         mathcursor->insert(MathAtom(new RefInset(ev.argument)));
741                 }
742                 updateLocal(bv, true);
743                 break;
744
745         default:
746                 result = UNDISPATCHED;
747         }
748
749         mathcursor->normalize();
750         mathcursor->touch();
751
752         lyx::Assert(mathcursor);
753
754         if (mathcursor->selection() || was_selection)
755                 toggleInsetSelection(bv);
756
757         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
758             result == UNDISPATCHED) {
759                 fitInsetCursor(bv);
760                 showInsetCursor(bv);
761                 revealCodes(bv);
762         } else {
763                 releaseMathCursor(bv);
764                 bv->unlockInset(this);
765         }
766
767         return result;  // original version
768 }
769
770
771 void InsetFormulaBase::revealCodes(BufferView * bv) const
772 {
773         if (!mathcursor)
774                 return;
775         bv->owner()->message(mathcursor->info());
776
777 #if 0
778         // write something to the minibuffer
779         // translate to latex
780         mathcursor->markInsert();
781         ostringstream os;
782         write(NULL, os);
783         string str = os.str();
784         mathcursor->markErase();
785         string::size_type pos = 0;
786         string res;
787         for (string::iterator it = str.begin(); it != str.end(); ++it) {
788                 if (*it == '\n')
789                         res += ' ';
790                 else if (*it == '\0') {
791                         res += "  -X-  ";
792                         pos = it - str.begin();
793                 }
794                 else
795                         res += *it;
796         }
797         if (pos > 30)
798                 res = res.substr(pos - 30);
799         if (res.size() > 60)
800                 res = res.substr(0, 60);
801         bv->owner()->message(res);
802 #endif
803 }
804
805
806 Inset::Code InsetFormulaBase::lyxCode() const
807 {
808         return Inset::MATH_CODE;
809 }
810
811
812 int InsetFormulaBase::ylow() const
813 {
814         return yo_ - ascent(view(), font_);
815 }
816
817
818 int InsetFormulaBase::yhigh() const
819 {
820         return yo_ + descent(view(), font_);
821 }
822
823
824 int InsetFormulaBase::xlow() const
825 {
826         return xo_;
827 }
828
829
830 int InsetFormulaBase::xhigh() const
831 {
832         return xo_ + width(view(), font_);
833 }
834
835
836 /////////////////////////////////////////////////////////////////////
837
838
839 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
840                                      bool, bool)
841 {
842 #ifdef WITH_WARNINGS
843 #warning pretty ugly
844 #endif
845         static InsetFormulaBase * lastformula = 0;
846         static MathIterator current = MathIterator(ibegin(par().nucleus()));
847         static MathArray ar;
848         static string laststr;
849
850         if (lastformula != this || laststr != str) {
851                 //lyxerr << "reset lastformula to " << this << "\n";
852                 lastformula = this;
853                 laststr = str;
854                 current = ibegin(par().nucleus());
855                 ar.clear();
856                 mathed_parse_cell(ar, str);
857         } else {
858                 ++current;
859         }
860         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
861
862         for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
863                 if (it.cell().matchpart(ar, it.back().pos_)) {
864                         bv->unlockInset(bv->theLockingInset());
865                         if (!bv->lockInset(this)) {
866                                 lyxerr << "Cannot lock inset" << endl;
867                                 return false;
868                         }
869                         delete mathcursor;
870                         mathcursor = new MathCursor(this, true);
871                         metrics(bv);
872                         mathcursor->setSelection(it, ar.size());
873                         current = it;
874                         it.jump(ar.size());
875                         updateLocal(bv, false);
876                         return true;
877                 }
878         }
879
880         //lyxerr << "not found!\n";
881         lastformula = 0;
882         return false;
883 }
884
885
886 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
887                                       bool a, bool b)
888 {
889         lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
890         return searchForward(bv, what, a, b);
891 }
892
893
894 /////////////////////////////////////////////////////////////////////
895
896
897 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
898 {
899         if (!bv->available())
900                 return;
901
902         // use selection if available..
903         //string sel;
904         //if (action == LFUN_MATH_IMPORT_SELECTION)
905         //      sel = "";
906         //else
907
908         string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
909
910         if (sel.empty()) {
911                 InsetFormula * f = new InsetFormula(bv);
912                 if (openNewInset(bv, f)) {
913                         f->mutate("simple");
914                         // don't do that also for LFUN_MATH_MODE unless you want end up with
915                         // always changing to mathrm when opening an inlined inset
916                         // -- I really hate "LyXfunc overloading"...
917                         if (display)
918                                 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
919                         f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, arg));
920                 }
921         } else {
922                 // create a macro if we see "\\newcommand" somewhere, and an ordinary
923                 // formula otherwise
924                 InsetFormulaBase * f;
925                 if (sel.find("\\newcommand") == string::npos &&
926                                 sel.find("\\def") == string::npos)
927                         f = new InsetFormula(sel);
928                 else
929                         f = new InsetFormulaMacro(sel);
930                 bv->getLyXText()->cutSelection(bv);
931                 openNewInset(bv, f);
932         }
933         bv->owner()->getLyXFunc().setMessage(N_("Math editor mode"));
934 }
935
936
937 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
938 {
939         mathDispatchCreation(bv, arg, true);
940 }
941
942
943 void mathDispatchMathMode(BufferView * bv, string const & arg)
944 {
945         mathDispatchCreation(bv, arg, false);
946 }
947
948
949 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
950 {
951         mathDispatchCreation(bv, arg, true);
952 }
953
954
955 void mathDispatchMathMacro(BufferView * bv, string const & arg)
956 {
957         if (!bv->available())
958                 return;
959         if (arg.empty())
960                 bv->owner()->getLyXFunc().setErrorMessage(N_("Missing argument"));
961         else {
962                 string s = arg;
963                 string const s1 = token(s, ' ', 1);
964                 int const na = s1.empty() ? 0 : lyx::atoi(s1);
965                 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
966         }
967 }
968
969
970 void mathDispatchMathDelim(BufferView * bv, string const & arg)
971 {
972         if (!bv->available())
973                 return;
974         InsetFormula * f = new InsetFormula(bv);
975         if (openNewInset(bv, f)) {
976                 f->mutate("simple");
977                 bv->theLockingInset()->localDispatch(FuncRequest(bv, LFUN_MATH_DELIM, arg));
978         }
979 }
980
981
982 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
983 {
984         if (!bv->available())
985                 return;
986         InsetFormula * f = new InsetFormula(bv);
987         if (openNewInset(bv, f)) {
988                 f->mutate("simple");
989                 bv->theLockingInset()->localDispatch(FuncRequest(bv, LFUN_INSERT_MATRIX, arg));
990         }
991 }
992
993
994 void mathDispatchInsertMath(BufferView * bv, string const & arg)
995 {
996         if (!bv->available())
997                 return;
998         InsetFormula * f = new InsetFormula(bv);
999         if (openNewInset(bv, f)) {
1000                 f->mutate("simple");
1001     bv->theLockingInset()->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, arg));
1002         }
1003 }
1004
1005
1006 void mathDispatchGreek(BufferView * bv, string const & arg)
1007 {
1008         if (!bv->available())
1009                 return;
1010         InsetFormula * f = new InsetFormula(bv);
1011         if (openNewInset(bv, f)) {
1012                 f->mutate("simple");
1013                 bv->theLockingInset()->localDispatch(FuncRequest(bv, LFUN_GREEK, arg));
1014                 bv->unlockInset(f);
1015         }
1016 }
1017
1018
1019 void mathDispatch(BufferView *, kb_action, string const &)
1020 {}
1021
1022
1023 void mathDispatch(BufferView *, string const &)
1024 {}