]> git.lyx.org Git - features.git/blob - src/mathed/formulabase.C
b68844fa55391112895afa06584d13e8f8e3f6f1
[features.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/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_scopeinset.h"
43 #include "math_macrotable.h"
44 #include "support/lyxlib.h"
45 #include "mathed/support.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->handleAccent(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 int InsetFormulaBase::latex(Buffer const *, ostream & os,
135         bool fragile, bool spacing) const
136 {
137         return latex(os, fragile, spacing);
138 }
139
140 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
141 {
142         return ascii(os, spacing);
143 }
144
145 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
146 {
147         return linuxdoc(os);
148 }
149
150 int InsetFormulaBase::docBook(Buffer const *, ostream & os) const
151 {
152         return docBook(os);
153 }
154
155
156
157 // Check if uses AMS macros
158 void InsetFormulaBase::validate(LaTeXFeatures &) const
159 {}
160
161
162 string const InsetFormulaBase::editMessage() const
163 {
164         return _("Math editor mode");
165 }
166
167
168 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
169 {
170         mathcursor = new MathCursor(this);
171
172         if (!bv->lockInset(this))
173                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
174
175         metrics();
176         //bv->updateInset(this, false);
177         if (x == 0)
178                 mathcursor->first();
179         else
180                 mathcursor->last();
181         sel_x = 0;
182         sel_y = 0;
183         sel_flag = false;
184 }
185
186
187 void InsetFormulaBase::edit(BufferView * bv, bool front)
188 {
189         edit(bv, front ? 0 : 1, 0, 0);
190 }
191
192
193 void InsetFormulaBase::insetUnlock(BufferView * bv)
194 {
195         if (mathcursor) {
196                 if (mathcursor->inMacroMode()) {
197                         mathcursor->macroModeClose();
198                         updateLocal(bv, true);
199                 }
200                 delete mathcursor;
201                 mathcursor = 0;
202         }
203         bv->updateInset(this, false);
204 }
205
206
207 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
208 {
209         mathcursor->getPos(x, y);
210         x -= par()->xo();
211         y -= par()->yo();
212 }
213
214
215 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
216 {
217         if (!mathcursor)
218                 return;
219
220         if (isCursorVisible())
221                 bv->hideLockedInsetCursor();
222         else {
223                 int x;
224                 int y;
225                 mathcursor->getPos(x, y);
226                 //x -= par()->xo();
227                 y -= par()->yo();
228                 int asc;
229                 int desc;
230                 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
231                 bv->showLockedInsetCursor(x, y, asc, desc);
232         }
233
234         toggleCursorVisible();
235 }
236
237
238 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
239 {
240         if (!isCursorVisible()) {
241                 if (mathcursor) {
242                         int x;
243                         int y;
244                         mathcursor->getPos(x, y);
245                         x -= par()->xo();
246                         y -= par()->yo();
247                         int asc;
248                         int desc;
249                         math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
250                         bv->fitLockedInsetCursor(x, y, asc, desc);
251                 }
252                 toggleInsetCursor(bv);
253         }
254 }
255
256
257 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
258 {
259         if (isCursorVisible())
260                 toggleInsetCursor(bv);
261 }
262
263
264 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
265 {
266         if (mathcursor)
267                 bv->updateInset(this, false);
268 }
269
270
271 vector<string> const InsetFormulaBase::getLabelList() const
272 {
273   return std::vector<string>();
274 }
275
276
277 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
278 {
279         metrics();
280         bv->updateInset(this, dirty);
281 }
282
283
284 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
285                                           int x, int y, int /*button*/)
286 {
287         if (mathcursor) {
288                 hideInsetCursor(bv);
289                 x += par()->xo();
290                 y += par()->yo();
291                 mathcursor->setPos(x, y);
292                 showInsetCursor(bv);
293                 if (sel_flag) {
294                         sel_flag = false;
295                         sel_x = 0;
296                         sel_y = 0;
297                 }
298                 bv->updateInset(this, false);
299         }
300 }
301
302
303 void InsetFormulaBase::insetButtonPress(BufferView * bv,
304                                         int x, int y, int /*button*/)
305 {
306         sel_flag = false;
307         sel_x = x;
308         sel_y = y;
309         if (mathcursor && mathcursor->selection()) {
310                 mathcursor->selClear();
311                 bv->updateInset(this, false);
312         }
313 }
314
315
316 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
317                                          int x, int y, int /*button*/)
318 {
319         if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
320                 sel_flag = true;
321                 hideInsetCursor(bv);
322                 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
323                 mathcursor->selStart();
324                 showInsetCursor(bv);
325                 mathcursor->getPos(sel_x, sel_y);
326         } else if (sel_flag) {
327                 hideInsetCursor(bv);
328                 x += par()->xo();
329                 y += par()->yo();
330                 mathcursor->setPos(x, y);
331                 showInsetCursor(bv);
332                 mathcursor->getPos(x, y);
333                 if (sel_x != x || sel_y != y)
334                         bv->updateInset(this, false);
335                 sel_x = x;
336                 sel_y = y;
337         }
338 }
339
340
341 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
342 {
343         lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
344 }
345
346
347
348 UpdatableInset::RESULT
349 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
350                             string const & arg)
351 {
352         //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
353         //      << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
354
355         static int greek_kb_flag = 0;
356
357         if (!mathcursor) 
358                 return UNDISPATCHED;
359
360         MathTextCodes varcode = LM_TC_MIN;
361         bool was_macro = mathcursor->inMacroMode();
362         bool sel = false;
363         bool was_selection = mathcursor->selection();
364         RESULT result = DISPATCHED;
365
366         hideInsetCursor(bv);
367
368         if (mathcursor->getLastCode() == LM_TC_TEX)
369                 varcode = LM_TC_TEX;
370
371         mathcursor->normalize();
372
373         switch (action) {
374
375                 // --- Cursor Movements ---------------------------------------------
376
377         case LFUN_RIGHTSEL:
378                 sel = true; // fall through...
379
380         case LFUN_RIGHT:
381                 result = DISPATCH_RESULT(mathcursor->right(sel));
382                 updateLocal(bv, false);
383                 break;
384
385
386         case LFUN_LEFTSEL:
387                 sel = true; // fall through
388
389         case LFUN_LEFT:
390                 result = DISPATCH_RESULT(mathcursor->left(sel));
391                 updateLocal(bv, false);
392                 break;
393
394
395         case LFUN_UPSEL:
396                 sel = true;
397
398         case LFUN_UP:
399                 result = DISPATCH_RESULT(mathcursor->up(sel));
400                 updateLocal(bv, false);
401                 break;
402
403
404         case LFUN_DOWNSEL:
405                 sel = true;
406
407         case LFUN_DOWN:
408                 result = DISPATCH_RESULT(mathcursor->down(sel));
409                 updateLocal(bv, false);
410                 break;
411
412         case LFUN_HOME:
413                 mathcursor->home();
414                 updateLocal(bv, false);
415                 break;
416
417         case LFUN_END:
418                 mathcursor->end();
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_BACKSPACE:
445                 bv->lockedInsetStoreUndo(Undo::DELETE);
446                 mathcursor->backspace();
447                 bv->updateInset(this, true);
448                 break;
449
450         case LFUN_DELETE:
451                 bv->lockedInsetStoreUndo(Undo::DELETE);
452                 mathcursor->erase();
453                 bv->updateInset(this, true);
454                 break;
455
456                 //    case LFUN_GETXY:
457                 //      sprintf(dispatch_buffer, "%d %d",);
458                 //      dispatch_result = dispatch_buffer;
459                 //      break;
460         case LFUN_SETXY:
461         {
462                 lyxerr << "LFUN_SETXY broken!\n";
463                 int x;
464                 int y;
465                 int x1;
466                 int y1;
467                 istringstream is(arg.c_str());
468                 is >> x >> y;
469                 par()->getXY(x1, y1);
470                 mathcursor->setPos(x1 + x, y1 + y);
471                 updateLocal(bv, false);
472         }
473         break;
474
475
476         case LFUN_PASTE:
477                 if (was_macro)
478                         mathcursor->macroModeClose();
479                 bv->lockedInsetStoreUndo(Undo::INSERT);
480                 mathcursor->selPaste();
481                 updateLocal(bv, true);
482                 break;
483
484         case LFUN_CUT:
485                 bv->lockedInsetStoreUndo(Undo::DELETE);
486                 mathcursor->selCut();
487                 updateLocal(bv, true);
488                 break;
489
490         case LFUN_COPY:
491                 mathcursor->selCopy();
492                 break;
493
494         case LFUN_HOMESEL:
495         case LFUN_ENDSEL:
496         case LFUN_WORDRIGHTSEL:
497         case LFUN_WORDLEFTSEL:
498                 break;
499
500                 // --- accented characters ------------------------------
501
502         case LFUN_UMLAUT:     handleAccent(bv, "ddot"); break;
503         case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
504         case LFUN_GRAVE:      handleAccent(bv, "grave"); break;
505         case LFUN_ACUTE:      handleAccent(bv, "acute"); break;
506         case LFUN_TILDE:      handleAccent(bv, "tilde"); break;
507         case LFUN_MACRON:     handleAccent(bv, "bar"); break;
508         case LFUN_DOT:        handleAccent(bv, "dot"); break;
509         case LFUN_CARON:      handleAccent(bv, "check"); break;
510         case LFUN_BREVE:      handleAccent(bv, "breve"); break;
511         case LFUN_VECTOR:     handleAccent(bv, "vec"); break;
512
513                 // Greek mode
514         case LFUN_GREEK:
515                 if (!greek_kb_flag) {
516                         greek_kb_flag = 1;
517                         bv->owner()->message(_("Math greek mode on"));
518                 } else
519                         greek_kb_flag = 0;
520                 break;
521
522                 // Greek keyboard
523         case LFUN_GREEK_TOGGLE:
524                 greek_kb_flag = greek_kb_flag ? 0 : 2;
525                 if (greek_kb_flag)
526                         bv->owner()->message(_("Math greek keyboard on"));
527                 else
528                         bv->owner()->message(_("Math greek keyboard off"));
529                 break;
530
531                 //  Math fonts
532         case LFUN_BOLD:    handleFont(bv, LM_TC_BF); break;
533         case LFUN_SANS:    handleFont(bv, LM_TC_SF); break;
534         case LFUN_EMPH:    handleFont(bv, LM_TC_CAL); break;
535         case LFUN_ROMAN:   handleFont(bv, LM_TC_RM); break;
536         case LFUN_CODE:    handleFont(bv, LM_TC_TT); break;
537         case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
538
539         case LFUN_MATH_MODE:
540                 handleFont(bv, LM_TC_TEXTRM);
541                 //bv->owner()->message(_("math text mode toggled"));
542                 break;
543
544         case LFUN_MATH_LIMITS:
545                 bv->lockedInsetStoreUndo(Undo::INSERT);
546                 if (mathcursor->toggleLimits())
547                         updateLocal(bv, true);
548                 break;
549
550         case LFUN_MATH_SIZE:
551                 if (!arg.empty()) {
552                         bv->lockedInsetStoreUndo(Undo::INSERT);
553                         latexkeys const * l = in_word_set(arg);
554                         mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
555                         updateLocal(bv, true);
556                 }
557                 break;
558
559         case LFUN_INSERT_MATRIX:
560                 if (!arg.empty()) {
561                         bv->lockedInsetStoreUndo(Undo::INSERT);
562                         mathcursor->interpret("matrix " + arg);
563                         updateLocal(bv, true);
564                 }
565                 break;
566
567         case LFUN_INSERT_MATH:
568                 if (!arg.empty()) {
569                         bv->lockedInsetStoreUndo(Undo::INSERT);
570                         mathcursor->interpret(arg);
571                         updateLocal(bv, true);
572                 }
573                 break;
574
575         case LFUN_MATH_SPACE:
576         {
577                 bv->lockedInsetStoreUndo(Undo::EDIT);
578                 MathSpaceInset * p = mathcursor->prevSpaceInset();
579                 if (p) 
580                         p->incSpace();
581                 else
582                         mathcursor->insert(new MathSpaceInset(1));
583                 updateLocal(bv, true);
584                 break;
585         }
586
587         case LFUN_MATH_DELIM:
588         {
589                 bv->lockedInsetStoreUndo(Undo::INSERT);
590                 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
591                 string ls;
592                 string rs;
593                 istringstream is(arg.c_str());
594                 is >> ls >> rs;
595                 latexkeys const * l = in_word_set(ls);
596                 latexkeys const * r = in_word_set(rs);
597                 if (!is || !l || !r) {
598                         lyxerr << "can't parse delimeters from '" << arg << "'\n";
599                         break;
600                 }
601                 bv->lockedInsetStoreUndo(Undo::EDIT);
602                 mathcursor->handleDelim(l, r);
603                 updateLocal(bv, true);
604                 break;
605         }
606
607         case LFUN_PROTECTEDSPACE:
608                 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
609                 bv->lockedInsetStoreUndo(Undo::INSERT);
610                 mathcursor->insert(new MathSpaceInset(1));
611                 updateLocal(bv, true);
612                 break;
613
614         case LFUN_UNDO:
615                 bv->owner()->message(_("Invalid action in math mode!"));
616                 break;
617
618
619         case LFUN_MATH_HALIGN:
620         {
621                 bv->lockedInsetStoreUndo(Undo::INSERT);
622                 lyxerr << "handling halign '" << arg << "'\n";
623                 int idx;
624                 MathArrayInset * p = matrixpar(idx);
625                 if (!p)
626                         break; 
627                 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
628                 updateLocal(bv, true);
629                 break;
630         }
631
632         case LFUN_MATH_VALIGN:
633         {
634                 bv->lockedInsetStoreUndo(Undo::INSERT);
635                 lyxerr << "handling valign '" << arg << "'\n";
636                 int idx;
637                 MathArrayInset * p = matrixpar(idx);
638                 if (!p)
639                         break; 
640                 p->valign(arg.size() ? arg[0] : 'c');
641                 updateLocal(bv, true);
642                 break;
643         }
644
645         case LFUN_MATH_ROW_INSERT:
646         {
647                 bv->lockedInsetStoreUndo(Undo::INSERT);
648                 int idx;
649                 MathArrayInset * p = matrixpar(idx);
650                 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
651                 if (!p)
652                         break; 
653                 p->addRow(p->row(idx));
654                 updateLocal(bv, true);
655                 break;
656         }
657
658         case LFUN_MATH_ROW_DELETE:
659         {
660                 bv->lockedInsetStoreUndo(Undo::INSERT);
661                 int idx;
662                 MathArrayInset * p = matrixpar(idx);
663                 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
664                 if (!p)
665                         break; 
666                 p->delRow(p->row(idx));
667                 updateLocal(bv, true);
668                 break;
669         }
670
671         case LFUN_MATH_COLUMN_INSERT:
672         {
673                 bv->lockedInsetStoreUndo(Undo::INSERT);
674                 int idx;
675                 MathArrayInset * p = matrixpar(idx);
676                 if (!p)
677                         break; 
678                 p->addCol(p->col(idx));
679                 updateLocal(bv, true);
680                 break;
681         }
682
683         case LFUN_MATH_COLUMN_DELETE:
684         {
685                 bv->lockedInsetStoreUndo(Undo::INSERT);
686                 int idx;
687                 MathArrayInset * p = matrixpar(idx);
688                 if (!p)
689                         break; 
690                 p->delCol(p->col(idx));
691                 updateLocal(bv, true);
692                 break;
693         }
694
695         case LFUN_EXEC_COMMAND:
696                 result = UNDISPATCHED;
697                 break;
698
699         default:
700                 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
701                         unsigned char c = arg[0];
702
703                         lyxerr << "Action: " << action << endl;
704                         
705                         lyxerr << "char: '" << c << "'  int: " << int(c) << endl;
706                         //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);      
707                         //lyxerr << "trans: '" << c << "'  int: " << int(c) << endl;
708                         bv->lockedInsetStoreUndo(Undo::INSERT);
709
710                         if (c == 0) {      // Dead key, do nothing
711                                 //lyxerr << "deadkey" << endl;
712                                 break;
713                         }
714
715                         if (isalpha(c)) {
716                                 if (mathcursor->getLastCode() == LM_TC_TEX) {
717                                         mathcursor->macroModeOpen();
718                                         mathcursor->clearLastCode();
719                                         varcode = LM_TC_MIN;
720                                 } else if (!varcode) {          
721                                         MathTextCodes f = mathcursor->getLastCode() ?
722                                                 mathcursor->getLastCode() :
723                                                 mathcursor->nextCode();
724                                         varcode = MathIsAlphaFont(f) ?
725                                                 static_cast<MathTextCodes>(f) :
726                                                 LM_TC_VAR;
727                                 }
728                                 
729                                 //           lyxerr << "Varcode << vardoce;
730                                 MathTextCodes char_code = varcode;
731                                 if (greek_kb_flag) {
732                                         char greek[26] =
733                                         {'A', 'B', 'X',  0 , 'E',  0 ,  0 , 'H', 'I',  0 ,
734                                          'K',  0 , 'M', 'N', 'O',  0 ,  0 , 'P',  0 , 'T',
735                                          0,  0,   0,   0,   0 , 'Z' };
736                                         
737                                         if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
738                                                 char_code = LM_TC_RM;
739                                                 c = greek[c - 'A'];
740                                         } else
741                                                 char_code = LM_TC_SYMB;
742                                 }
743                                 
744                                 mathcursor->insert(c, char_code);
745                                 
746                                 if (greek_kb_flag && char_code == LM_TC_RM)
747                                         mathcursor->setLastCode(LM_TC_VAR);
748                                 
749                                 varcode = LM_TC_MIN;
750                                 
751                                 if (greek_kb_flag < 2)
752                                         greek_kb_flag = 0;
753                                 
754                         } else if (c == '{') {
755                                 mathcursor->insert(new MathScopeInset);
756                                 mathcursor->left();
757                                 mathcursor->clearLastCode();
758                         } else if (strchr("!,:;", c) && (varcode == LM_TC_TEX||was_macro)) {
759                                 mathcursor->insert(c, LM_TC_TEX);
760                                 mathcursor->clearLastCode();
761                         } else if (c == '_' && varcode == LM_TC_TEX) {
762                                 mathcursor->insert(c, LM_TC_SPECIAL);
763                                 mathcursor->clearLastCode();
764                         } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
765                                 mathcursor->macroModeOpen();
766                                 mathcursor->clearLastCode();
767                                 mathcursor->insert(c, LM_TC_MIN);
768                         } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
769                                 MathTextCodes code = mathcursor->getLastCode();
770                                 if (code != LM_TC_TEXTRM)
771                                         code = LM_TC_CONST;
772                                 mathcursor->insert(c, code);
773                         } else if (strchr("+/-*<>=", c)) {
774                                 MathTextCodes code = mathcursor->getLastCode();
775                                 if (code != LM_TC_TEXTRM)
776                                         code = LM_TC_BOP;
777                                 mathcursor->insert(c, code);
778                         } else if (strchr("#$%{|}", c)) {
779                                 MathTextCodes code = mathcursor->getLastCode();
780                                 if (code != LM_TC_TEXTRM)
781                                         code = LM_TC_SPECIAL;
782                                 mathcursor->insert(c, code);
783                         } else if (c == '_' || c == '^') {
784                                 char s[2];
785                                 s[0] = c;
786                                 s[1] = 0;
787                                 mathcursor->interpret(s);
788                         } else if (c == ' ') {
789                                 if (!varcode) { 
790                                         MathTextCodes f = (mathcursor->getLastCode()) ?
791                                                 mathcursor->getLastCode() :
792                                                 mathcursor->nextCode();
793                                         varcode = MathIsAlphaFont(f) ? f : LM_TC_VAR;
794                                 }
795                                 
796                                 if (varcode == LM_TC_TEXTRM)
797                                         mathcursor->insert(c, LM_TC_TEXTRM);
798                                 else if (was_macro)
799                                         mathcursor->macroModeClose();
800                                 else if (mathcursor->popRight())
801                                         ;
802                                 else {
803                                         // this would not work if the inset is in an table!
804                                         //bv->text->cursorRight(bv, true);
805                                         result = FINISHED;
806                                 }
807                         } else if (c == '\'' || c == '@') {
808                                 mathcursor->insert(c, LM_TC_VAR);
809                         } else if (c == '\\') {
810                                 if (was_macro)
811                                         mathcursor->macroModeClose();
812                                 bv->owner()->message(_("TeX mode"));
813                                 mathcursor->setLastCode(LM_TC_TEX);
814                         }
815                         updateLocal(bv, true);
816                 } else if (action == LFUN_MATH_PANEL) {
817                         result = UNDISPATCHED;
818                 } else {
819                         lyxerr << "Closed by action " << action << endl;
820                         result =  FINISHED;
821                 }
822         }
823
824         mathcursor->normalize();
825
826         if (was_macro != mathcursor->inMacroMode()
827                                 && action >= 0 && action != LFUN_BACKSPACE) 
828                 updateLocal(bv, true);
829         
830         if (mathcursor->selection() || was_selection)
831                 toggleInsetSelection(bv);
832
833         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
834             result == UNDISPATCHED)
835                 showInsetCursor(bv);
836         else
837                 bv->unlockInset(this);
838
839         return result;  // original version
840 }
841
842
843 Inset::Code InsetFormulaBase::lyxCode() const
844 {
845         return Inset::MATH_CODE;
846 }
847
848
849 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
850 {
851         if (bv->available()) {
852 // Feature "Read math inset from selection" disabled.
853 //              // use selection if available..
854 //              string sel;
855 //              if (action == LFUN_MATH_IMPORT_SELECTION)
856 //                      sel = "";
857 //              else
858 //                      sel = bv->getLyXText()->selectionAsString(bv->buffer());
859
860                         InsetFormula * f;
861 //              if (sel.empty()) {
862                                 f = new InsetFormula;
863                                 if (openNewInset(bv, f)) {
864                                         // don't do that also for LFUN_MATH_MODE unless you want end up with
865                                         // always changing to mathrm when opening an inlined inset
866                                         // -- I really hate "LyXfunc overloading"...
867                                         if (display)
868                                                 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
869                                         f->localDispatch(bv, LFUN_INSERT_MATH, arg);
870                                 }
871 //              } else {
872 //                      f = new InsetFormula(sel);
873 //                      bv->getLyXText()->cutSelection(bv);
874 //                      openNewInset(bv, f);
875 //              }
876         }
877         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
878 }
879
880 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
881 {
882         mathDispatchCreation(bv, arg, true);
883 }
884         
885 void mathDispatchMathMode(BufferView * bv, string const & arg)
886 {
887         mathDispatchCreation(bv, arg, false);
888 }
889
890 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
891 {
892         mathDispatchCreation(bv, arg, true);
893 }
894
895 void mathDispatchMathMacro(BufferView * bv, string const & arg)
896 {
897         if (bv->available()) {
898                 if (arg.empty())
899                         bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
900                 else {
901                         string s(arg);
902                         string const s1 = token(s, ' ', 1);
903                         int const na = s1.empty() ? 0 : lyx::atoi(s1);
904                         openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
905                 }
906         }
907 }
908
909 void mathDispatchMathDelim(BufferView * bv, string const & arg)
910 {          
911         if (bv->available()) { 
912                 if (openNewInset(bv, new InsetFormula))
913                         bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
914         }
915 }          
916
917
918 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
919 {          
920         if (bv->available()) { 
921                 if (openNewInset(bv, new InsetFormula))
922                         bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
923         }
924 }          
925
926 void mathDispatchInsertMath(BufferView * bv, string const & arg)
927 {
928         if (bv->available()) {
929                 if (arg.size() && arg[0] == '\\')
930                         openNewInset(bv, new InsetFormula(arg));
931                 else
932                         mathDispatchMathMode(bv, arg);
933         }
934 }
935