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