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