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