]> git.lyx.org Git - features.git/blob - src/mathed/formulabase.C
halfway through fixing size of math in non-standard sizesd paragraohs like
[features.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 "commandtags.h"
28 #include "math_cursor.h"
29 #include "math_parser.h"
30 #include "BufferView.h"
31 #include "lyxtext.h"
32 #include "lyxfunc.h"
33 #include "gettext.h"
34 #include "LaTeXFeatures.h"
35 #include "debug.h"
36 #include "support.h"
37 #include "support/lstrings.h"
38 #include "LyXView.h"
39 #include "Painter.h"
40 #include "font.h"
41 #include "math_arrayinset.h"
42 #include "math_spaceinset.h"
43 #include "math_macrotable.h"
44 #include "math_factory.h"
45 #include "support/lyxlib.h"
46 #include "undo_funcs.h"
47
48 using std::endl;
49 using std::ostream;
50 using std::vector;
51
52 extern char const * latex_mathenv[];
53 MathCursor        * mathcursor = 0;
54
55
56 namespace {
57
58
59 // local global
60 int sel_x;
61 int sel_y;
62 bool sel_flag;
63
64 void handleFont(BufferView * bv, MathTextCodes t) 
65 {
66         if (mathcursor->selection())
67                 bv->lockedInsetStoreUndo(Undo::EDIT);
68         mathcursor->handleFont(t);
69 }
70
71
72 void handleAccent(BufferView * bv, string const & name)
73 {
74         bv->lockedInsetStoreUndo(Undo::EDIT);
75         mathcursor->insert(createMathInset(name));
76 }
77
78
79 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
80 {
81         LyXText * lt = bv->getLyXText();
82         
83         bv->beforeChange(lt);
84         finishUndo();
85         if (!bv->insertInset(new_inset)) {
86                 delete new_inset;
87                 return false;
88         }
89         new_inset->edit(bv, 0, 0, 0);
90         return true;
91 }
92
93
94 // returns the nearest enclosing grid
95 MathArrayInset * matrixpar(MathInset::idx_type & idx)
96 {
97         idx = 0;
98         return (mathcursor ? mathcursor->enclosingArray(idx) : 0); 
99 }
100
101
102 } // namespace anon
103
104
105
106 InsetFormulaBase::InsetFormulaBase()
107         : view_(0), font_(0)
108 {
109         // This is needed as long the math parser is not re-entrant
110         MathMacroTable::builtinMacros();
111         //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
112 }
113
114
115 // Check if uses AMS macros
116 void InsetFormulaBase::validate(LaTeXFeatures &) const
117 {}
118
119
120 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const * f) const 
121 {
122         if (bv)
123                 view_ = bv;
124         if (f)
125                 font_ = f;
126         if (f)
127                 lyxerr << "fontsize: " << f->size() << "\n";
128         MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
129         par()->metrics(mi);
130 }
131
132
133 string const InsetFormulaBase::editMessage() const
134 {
135         return _("Math editor mode");
136 }
137
138
139 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
140 {
141         if (!bv->lockInset(this))
142                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
143
144         mathcursor = new MathCursor(this, x == 0);
145         metrics(bv);
146         // if that is removed, we won't get the magenta box when entering an
147         // inset for the first time
148         bv->updateInset(this, false);
149         sel_x = 0;
150         sel_y = 0;
151         sel_flag = false;
152 }
153
154
155 void InsetFormulaBase::edit(BufferView * bv, bool front)
156 {
157         // looks hackish but seems to work
158         edit(bv, front ? 0 : 1, 0, 0);
159 }
160
161
162 void InsetFormulaBase::insetUnlock(BufferView * bv)
163 {
164         if (mathcursor) {
165                 if (mathcursor->inMacroMode()) {
166                         mathcursor->macroModeClose();
167                         updateLocal(bv, true);
168                 }
169                 delete mathcursor;
170                 mathcursor = 0;
171         }
172         bv->updateInset(this, false);
173 }
174
175
176 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
177 {
178         mathcursor->getPos(x, y);
179         x -= par()->xo();
180         y -= par()->yo();
181         y -= 3;
182 }
183
184
185 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
186 {
187         if (!mathcursor)
188                 return;
189
190         if (isCursorVisible())
191                 bv->hideLockedInsetCursor();
192         else {
193                 int x;
194                 int y;
195                 mathcursor->getPos(x, y);
196                 //x -= par()->xo();
197                 y -= par()->yo();
198                 y -= 3;
199                 int asc = 0;
200                 int des = 0;
201                 MathMetricsInfo mi(bv, font_, LM_ST_TEXT);
202                 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
203                 bv->showLockedInsetCursor(x, y, asc, des);
204         }
205
206         toggleCursorVisible();
207 }
208
209
210 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
211 {
212         if (!isCursorVisible()) {
213                 if (mathcursor) {
214                         int x;
215                         int y;
216                         mathcursor->getPos(x, y);
217                         x -= par()->xo();
218                         y -= par()->yo();
219                         int asc = 0;
220                         int des = 0;
221                         MathMetricsInfo mi(bv, font_, LM_ST_TEXT);
222                         math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
223                         bv->fitLockedInsetCursor(x, y, asc, des);
224                 }
225                 toggleInsetCursor(bv);
226         }
227 }
228
229
230 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
231 {
232         if (isCursorVisible())
233                 toggleInsetCursor(bv);
234 }
235
236
237 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
238 {
239         if (mathcursor)
240                 bv->updateInset(this, false);
241 }
242
243
244 vector<string> const InsetFormulaBase::getLabelList() const
245 {
246   return std::vector<string>();
247 }
248
249
250 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
251 {
252         metrics(bv);
253         bv->updateInset(this, dirty);
254 }
255
256
257 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
258                                           int x, int y, int /*button*/)
259 {
260         if (mathcursor) {
261                 hideInsetCursor(bv);
262                 x += par()->xo();
263                 y += par()->yo();
264                 mathcursor->setPos(x, y);
265                 showInsetCursor(bv);
266                 if (sel_flag) {
267                         sel_flag = false;
268                         sel_x = 0;
269                         sel_y = 0;
270                 }
271                 bv->updateInset(this, false);
272         }
273 }
274
275
276 void InsetFormulaBase::insetButtonPress(BufferView * bv,
277                                         int x, int y, int /*button*/)
278 {
279         sel_flag = false;
280         sel_x = x;
281         sel_y = y;
282         if (mathcursor && mathcursor->selection()) {
283                 mathcursor->selClear();
284                 bv->updateInset(this, false);
285         }
286 }
287
288
289 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
290                                          int x, int y, int /*button*/)
291 {
292         if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
293                 sel_flag = true;
294                 hideInsetCursor(bv);
295                 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
296                 mathcursor->selStart();
297                 showInsetCursor(bv);
298                 mathcursor->getPos(sel_x, sel_y);
299         } else if (sel_flag) {
300                 hideInsetCursor(bv);
301                 x += par()->xo();
302                 y += par()->yo();
303                 mathcursor->setPos(x, y);
304                 showInsetCursor(bv);
305                 mathcursor->getPos(x, y);
306                 if (sel_x != x || sel_y != y)
307                         bv->updateInset(this, false);
308                 sel_x = x;
309                 sel_y = y;
310         }
311 }
312
313
314 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
315 {
316         lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
317 }
318
319
320 UpdatableInset::RESULT
321 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
322                             string const & arg)
323 {
324         //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
325         //      << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
326
327         if (!mathcursor) 
328                 return UNDISPATCHED;
329
330         RESULT result      = DISPATCHED;
331         bool sel           = false;
332         bool was_macro     = mathcursor->inMacroMode();
333         bool was_selection = mathcursor->selection();
334
335         hideInsetCursor(bv);
336
337         mathcursor->normalize();
338
339         switch (action) {
340
341                 // --- Cursor Movements ---------------------------------------------
342
343         case LFUN_RIGHTSEL:
344                 sel = true; // fall through...
345
346         case LFUN_RIGHT:
347                 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
348                 updateLocal(bv, false);
349                 break;
350
351
352         case LFUN_LEFTSEL:
353                 sel = true; // fall through
354
355         case LFUN_LEFT:
356                 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
357                 updateLocal(bv, false);
358                 break;
359
360
361         case LFUN_UPSEL:
362                 sel = true;
363
364         case LFUN_UP:
365                 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
366                 updateLocal(bv, false);
367                 break;
368
369
370         case LFUN_DOWNSEL:
371                 sel = true;
372
373         case LFUN_DOWN:
374                 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
375                 updateLocal(bv, false);
376                 break;
377
378         case LFUN_HOMESEL:
379                 sel = true;
380
381         case LFUN_HOME:
382                 mathcursor->home(sel);
383                 updateLocal(bv, false);
384                 break;
385
386         case LFUN_ENDSEL:
387                 sel = true;
388
389         case LFUN_END:
390                 mathcursor->end(sel);
391                 updateLocal(bv, false);
392                 break;
393
394         case LFUN_DELETE_LINE_FORWARD:
395                 bv->lockedInsetStoreUndo(Undo::DELETE);
396                 mathcursor->delLine();
397                 updateLocal(bv, true);
398                 break;
399
400         case LFUN_TAB:
401                 mathcursor->idxNext();
402                 updateLocal(bv, false);
403                 break;
404
405         case LFUN_SHIFT_TAB:
406                 mathcursor->idxPrev();
407                 updateLocal(bv, false);
408                 break;
409
410         case LFUN_TABINSERT:
411                 bv->lockedInsetStoreUndo(Undo::EDIT);
412                 mathcursor->splitCell();
413                 updateLocal(bv, true);
414                 break;
415
416         case LFUN_BACKSPACE:
417                 bv->lockedInsetStoreUndo(Undo::DELETE);
418                 mathcursor->backspace();
419                 bv->updateInset(this, true);
420                 break;
421
422         case LFUN_DELETE:
423                 bv->lockedInsetStoreUndo(Undo::DELETE);
424                 mathcursor->erase();
425                 bv->updateInset(this, true);
426                 break;
427
428                 //    case LFUN_GETXY:
429                 //      sprintf(dispatch_buffer, "%d %d",);
430                 //      dispatch_result = dispatch_buffer;
431                 //      break;
432         case LFUN_SETXY: {
433                 lyxerr << "LFUN_SETXY broken!\n";
434                 int x;
435                 int y;
436                 int x1;
437                 int y1;
438                 istringstream is(arg.c_str());
439                 is >> x >> y;
440                 par()->getXY(x1, y1);
441                 mathcursor->setPos(x1 + x, y1 + y);
442                 updateLocal(bv, false);
443                 break;
444         }
445
446         case LFUN_PASTE:
447                 if (was_macro)
448                         mathcursor->macroModeClose();
449                 bv->lockedInsetStoreUndo(Undo::INSERT);
450                 mathcursor->selPaste();
451                 updateLocal(bv, true);
452                 break;
453
454         case LFUN_CUT:
455                 bv->lockedInsetStoreUndo(Undo::DELETE);
456                 mathcursor->selCut();
457                 updateLocal(bv, true);
458                 break;
459
460         case LFUN_COPY:
461                 mathcursor->selCopy();
462                 break;
463
464         case LFUN_WORDRIGHTSEL:
465         case LFUN_WORDLEFTSEL:
466                 break;
467
468                 // --- accented characters ------------------------------
469
470         case LFUN_UMLAUT:       handleAccent(bv, "ddot"); break;
471         case LFUN_CIRCUMFLEX:   handleAccent(bv, "hat"); break;
472         case LFUN_GRAVE:        handleAccent(bv, "grave"); break;
473         case LFUN_ACUTE:        handleAccent(bv, "acute"); break;
474         case LFUN_TILDE:        handleAccent(bv, "tilde"); break;
475         case LFUN_MACRON:       handleAccent(bv, "bar"); break;
476         case LFUN_DOT:          handleAccent(bv, "dot"); break;
477         case LFUN_CARON:        handleAccent(bv, "check"); break;
478         case LFUN_BREVE:        handleAccent(bv, "breve"); break;
479         case LFUN_VECTOR:       handleAccent(bv, "vec"); break;
480
481         //  Math fonts
482         case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
483         case LFUN_BOLD:         handleFont(bv, LM_TC_BF); break;
484         case LFUN_SANS:         handleFont(bv, LM_TC_SF); break;
485         case LFUN_EMPH:         handleFont(bv, LM_TC_CAL); break;
486         case LFUN_ROMAN:        handleFont(bv, LM_TC_RM); break;
487         case LFUN_CODE:         handleFont(bv, LM_TC_TT); break;
488         case LFUN_NOUN:         handleFont(bv, LM_TC_BB); break;
489         case LFUN_DEFAULT:      handleFont(bv, LM_TC_VAR); break;
490
491         case LFUN_GREEK: 
492                 handleFont(bv, LM_TC_GREEK1);
493                 if (arg.size())
494                         mathcursor->interpret(arg[0]);
495                 break;
496
497         case LFUN_MATH_MODE:
498                 handleFont(bv, LM_TC_TEXTRM);
499                 //bv->owner()->message(_("math text mode toggled"));
500                 break;
501
502         case LFUN_MATH_LIMITS:
503                 bv->lockedInsetStoreUndo(Undo::INSERT);
504                 if (mathcursor->toggleLimits())
505                         updateLocal(bv, true);
506                 break;
507
508         case LFUN_MATH_SIZE:
509                 if (!arg.empty()) {
510                         bv->lockedInsetStoreUndo(Undo::INSERT);
511                         latexkeys const * l = in_word_set(arg);
512                         mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
513                         updateLocal(bv, true);
514                 }
515                 break;
516
517         case LFUN_INSERT_MATRIX:
518                 if (!arg.empty()) {
519                         bv->lockedInsetStoreUndo(Undo::INSERT);
520                         mathcursor->interpret("matrix " + arg);
521                         updateLocal(bv, true);
522                 }
523                 break;
524
525         case LFUN_MATH_SPACE:
526         {
527                 bv->lockedInsetStoreUndo(Undo::EDIT);
528                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
529                 updateLocal(bv, true);
530                 break;
531         }
532
533         case LFUN_MATH_DELIM:
534         {
535                 bv->lockedInsetStoreUndo(Undo::INSERT);
536                 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
537                 string ls;
538                 string rs;
539                 istringstream is(arg.c_str());
540                 is >> ls >> rs;
541                 if (!is) {
542                         lyxerr << "can't parse delimeters from '" << arg << "'\n";
543                         break;
544                 }
545                 bv->lockedInsetStoreUndo(Undo::EDIT);
546                 mathcursor->handleDelim(ls, rs);
547                 updateLocal(bv, true);
548                 break;
549         }
550
551         case LFUN_PROTECTEDSPACE:
552                 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
553                 bv->lockedInsetStoreUndo(Undo::INSERT);
554                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
555                 updateLocal(bv, true);
556                 break;
557
558         case LFUN_UNDO:
559                 bv->owner()->message(_("Invalid action in math mode!"));
560                 break;
561
562
563         case LFUN_MATH_HALIGN:
564         {
565                 bv->lockedInsetStoreUndo(Undo::INSERT);
566                 lyxerr << "handling halign '" << arg << "'\n";
567                 MathInset::idx_type idx;
568                 MathArrayInset * p = matrixpar(idx);
569                 if (!p)
570                         break; 
571                 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
572                 updateLocal(bv, true);
573                 break;
574         }
575
576         case LFUN_MATH_VALIGN:
577         {
578                 bv->lockedInsetStoreUndo(Undo::INSERT);
579                 lyxerr << "handling valign '" << arg << "'\n";
580                 MathInset::idx_type idx;
581                 MathArrayInset * p = matrixpar(idx);
582                 if (!p)
583                         break; 
584                 p->valign(arg.size() ? arg[0] : 'c');
585                 updateLocal(bv, true);
586                 break;
587         }
588
589         case LFUN_MATH_ROW_INSERT:
590         {
591                 bv->lockedInsetStoreUndo(Undo::INSERT);
592                 MathInset::idx_type idx;
593                 MathArrayInset * p = matrixpar(idx);
594                 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
595                 if (!p)
596                         break; 
597                 p->addRow(p->row(idx));
598                 updateLocal(bv, true);
599                 break;
600         }
601
602         case LFUN_MATH_ROW_DELETE:
603         {
604                 bv->lockedInsetStoreUndo(Undo::INSERT);
605                 MathInset::idx_type idx;
606                 MathArrayInset * p = matrixpar(idx);
607                 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
608                 if (!p)
609                         break; 
610                 p->delRow(p->row(idx));
611                 updateLocal(bv, true);
612                 break;
613         }
614
615         case LFUN_MATH_COLUMN_INSERT:
616         {
617                 bv->lockedInsetStoreUndo(Undo::INSERT);
618                 MathInset::idx_type idx;
619                 MathArrayInset * p = matrixpar(idx);
620                 if (!p)
621                         break; 
622                 p->addCol(p->col(idx));
623                 updateLocal(bv, true);
624                 break;
625         }
626
627         case LFUN_MATH_COLUMN_DELETE:
628         {
629                 bv->lockedInsetStoreUndo(Undo::INSERT);
630                 MathInset::idx_type idx;
631                 MathArrayInset * p = matrixpar(idx);
632                 if (!p)
633                         break; 
634                 p->delCol(p->col(idx));
635                 updateLocal(bv, true);
636                 break;
637         }
638
639         case LFUN_EXEC_COMMAND:
640                 result = UNDISPATCHED;
641                 break;
642
643         case -1:
644         case LFUN_INSERT_MATH:
645         case LFUN_SELFINSERT:
646                 if (!arg.empty()) {
647                         bv->lockedInsetStoreUndo(Undo::INSERT);
648                         mathcursor->interpret(arg);
649                         updateLocal(bv, true);
650                 }
651                 break;
652
653         case LFUN_MATH_PANEL:
654                 result = UNDISPATCHED;
655                 break;
656
657         default:
658                 result = UNDISPATCHED;
659         }
660
661         lyx::Assert(mathcursor);
662         //mathcursor->normalize();
663
664         if (//was_macro != mathcursor->inMacroMode() &&
665                                 action >= 0 && action != LFUN_BACKSPACE) 
666                 updateLocal(bv, true);
667         
668         if (mathcursor->selection() || was_selection)
669                 toggleInsetSelection(bv);
670
671         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
672             result == UNDISPATCHED)
673                 showInsetCursor(bv);
674         else
675                 bv->unlockInset(this);
676
677         return result;  // original version
678 }
679
680
681 Inset::Code InsetFormulaBase::lyxCode() const
682 {
683         return Inset::MATH_CODE;
684 }
685
686
687 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
688 {
689         if (bv->available()) {
690                 // use selection if available..
691                 //string sel;
692                 //if (action == LFUN_MATH_IMPORT_SELECTION)
693                 //      sel = "";
694                 //else
695
696                 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
697
698                 InsetFormulaBase * f;
699                 if (sel.empty()) {
700                         f = new InsetFormula;
701                         if (openNewInset(bv, f)) {
702                                 // don't do that also for LFUN_MATH_MODE unless you want end up with
703                                 // always changing to mathrm when opening an inlined inset
704                                 // -- I really hate "LyXfunc overloading"...
705                                 if (display)
706                                         f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
707                                 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
708                         }
709                 } else {
710                         // create a macro if we see "\\newcommand" somewhere, and an ordinary
711                         // formula otherwise
712                         if (sel.find("\\newcommand") == string::npos) 
713                                 f = new InsetFormula(sel);
714                         else
715                                 f = new InsetFormulaMacro(sel);
716                         bv->getLyXText()->cutSelection(bv);
717                         openNewInset(bv, f);
718                 }
719         }
720         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
721 }
722
723
724 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
725 {
726         mathDispatchCreation(bv, arg, true);
727 }
728
729         
730 void mathDispatchMathMode(BufferView * bv, string const & arg)
731 {
732         mathDispatchCreation(bv, arg, false);
733 }
734
735
736 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
737 {
738         mathDispatchCreation(bv, arg, true);
739 }
740
741
742 void mathDispatchMathMacro(BufferView * bv, string const & arg)
743 {
744         if (bv->available()) {
745                 if (arg.empty())
746                         bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
747                 else {
748                         string s(arg);
749                         string const s1 = token(s, ' ', 1);
750                         int const na = s1.empty() ? 0 : lyx::atoi(s1);
751                         openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
752                 }
753         }
754 }
755
756
757 void mathDispatchMathDelim(BufferView * bv, string const & arg)
758 {          
759         if (bv->available()) { 
760                 if (openNewInset(bv, new InsetFormula))
761                         bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
762         }
763 }          
764
765
766 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
767 {          
768         if (bv->available()) { 
769                 if (openNewInset(bv, new InsetFormula))
770                         bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
771         }
772 }          
773
774
775 void mathDispatchInsertMath(BufferView * bv, string const & arg)
776 {
777         if (bv->available()) {
778                 if (arg.size() && arg[0] == '\\') {
779                         InsetFormula * f = new InsetFormula(arg);
780                         if (!bv->insertInset(f))
781                                 delete f;
782                 } else
783                         mathDispatchMathMode(bv, arg);
784         }
785 }
786
787
788 void mathDispatchGreek(BufferView * bv, string const & arg)
789 {          
790         if (bv->available()) { 
791                 InsetFormula * f = new InsetFormula;
792                 if (openNewInset(bv, f)) {
793                         bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
794                         bv->unlockInset(f);
795                 }
796         }
797 }          
798