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