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