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