]> git.lyx.org Git - features.git/blob - src/mathed/formulabase.C
mathed95.diff
[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/LOstream.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_deliminset.h"
43 #include "support/lyxlib.h"
44 #include "mathed/support.h"
45
46 using std::endl;
47 using std::ostream;
48 using std::vector;
49
50 extern char const * latex_special_chars;
51
52 int greek_kb_flag = 0;
53 extern char const * latex_mathenv[];
54 LyXFont           * Math_Fonts = 0;
55 MathCursor        * mathcursor = 0;
56
57
58 namespace {
59
60
61 // local global
62 int sel_x;
63 int sel_y;
64 bool sel_flag;
65
66 void mathed_init_fonts();
67
68 string nicelabel(string const & label)
69 {
70         return "(" + (label.empty() ? "#" : label) + ")";
71 }
72
73 void handleFont(BufferView * bv, MathTextCodes t) 
74 {
75         if (mathcursor->Selection())
76                 bv->lockedInsetStoreUndo(Undo::EDIT);
77         mathcursor->handleFont(t);
78 }
79
80 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
81 {
82         LyXText * lt = bv->getLyXText();
83         
84         bv->beforeChange(lt);
85         lt->finishUndo();
86         if (!bv->insertInset(new_inset)) {
87                 delete new_inset;
88                 return false;
89         }
90         new_inset->edit(bv, 0, 0, 0);
91         return true;
92 }
93
94 } // namespaces
95
96
97
98 LyXFont WhichFont(short type, int size)
99 {
100         LyXFont f;
101         
102         if (!Math_Fonts)
103                 mathed_init_fonts();
104
105         switch (type) {
106         case LM_TC_SYMB:        
107                 f = Math_Fonts[2];
108                 break;
109
110         case LM_TC_BSYM:        
111                 f = Math_Fonts[2];
112                 break;
113
114         case LM_TC_VAR:
115         case LM_TC_IT:
116                 f = Math_Fonts[0];
117                 break;
118
119         case LM_TC_BF:
120                 f = Math_Fonts[3];
121                 break;
122
123         case LM_TC_SF:
124                 f = Math_Fonts[7];
125                 break;
126
127         case LM_TC_CAL:
128                 f = Math_Fonts[4];
129                 break;
130
131         case LM_TC_TT:
132                 f = Math_Fonts[5];
133                 break;
134
135         case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
136         case LM_TC_TEXTRM:
137         case LM_TC_RM:
138                 f = Math_Fonts[6];
139                 break;
140
141         default:
142                 f = Math_Fonts[1];
143                 break;
144         }
145
146         switch (size) {
147         case LM_ST_DISPLAY:
148                 if (type == LM_TC_BSYM) {
149                         f.incSize();
150                         f.incSize();
151                 }
152                 break;
153
154         case LM_ST_TEXT:
155                 break;
156
157         case LM_ST_SCRIPT:
158                 f.decSize();
159                 break;
160
161         case LM_ST_SCRIPTSCRIPT:
162                 f.decSize();
163                 f.decSize();
164                 break;
165
166         default:
167                 lyxerr << "Math Error: wrong font size: " << size << endl;
168                 break;
169         }
170
171         if (type != LM_TC_TEXTRM)
172                 f.setColor(LColor::math);
173
174         return f;
175 }
176
177
178 namespace {
179
180 void mathed_init_fonts()
181 {
182         Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
183         //at once (JMarc) rc
184
185         for (int i = 0 ; i < 8 ; ++i) {
186                 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
187         }
188
189         Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
190
191         Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
192
193         Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
194         Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
195
196         Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
197
198         Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
199         Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
200
201         Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
202
203         Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
204
205         Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
206 }
207
208
209 // returns the nearest enclosing matrix
210 MathArrayInset * matrixpar(int & idx)
211 {
212         idx = 0;
213         return
214                 static_cast<MathArrayInset *> 
215                         (mathcursor ? mathcursor->enclosing(LM_OT_MATRIX, idx) : 0); 
216 }
217
218
219 } // namespace anon
220
221
222 InsetFormulaBase::InsetFormulaBase(MathInset * par)
223         : par_(par)
224 {}
225
226
227 InsetFormulaBase::InsetFormulaBase(InsetFormulaBase const & f)
228         : UpdatableInset(f), par_(static_cast<MathInset *>(f.par_->clone()))
229 {}
230
231
232 InsetFormulaBase::~InsetFormulaBase()
233 {
234 #ifdef WITH_WARNINGS
235 #warning leak this for a while...
236 #endif
237         //delete par_;
238 }
239
240
241 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
242 {
243         read(lex);
244 }
245
246 void InsetFormulaBase::write(Buffer const *, ostream & os) const
247 {
248         write(os);
249 }
250
251 int InsetFormulaBase::latex(Buffer const *, ostream & os,
252         bool fragile, bool spacing) const
253 {
254         return latex(os, fragile, spacing);
255 }
256
257 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
258 {
259         return ascii(os, spacing);
260 }
261
262 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
263 {
264         return linuxdoc(os);
265 }
266
267 int InsetFormulaBase::docBook(Buffer const *, ostream & os) const
268 {
269         return docBook(os);
270 }
271
272
273
274 // Check if uses AMS macros
275 void InsetFormulaBase::validate(LaTeXFeatures &) const
276 {}
277
278
279 string const InsetFormulaBase::editMessage() const
280 {
281         return _("Math editor mode");
282 }
283
284
285 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
286 {
287         mathcursor = new MathCursor(this);
288
289         if (!bv->lockInset(this))
290                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
291
292         par_->Metrics(LM_ST_TEXT);
293         bv->updateInset(this, false);
294         if (x == 0) {
295                 mathcursor->first();
296         } else {
297                 mathcursor->last();
298         }
299         sel_x = 0;
300         sel_y = 0;
301         sel_flag = false;
302 }
303
304
305 void InsetFormulaBase::insetUnlock(BufferView * bv)
306 {
307         if (mathcursor) {
308                 if (mathcursor->InMacroMode()) {
309                         mathcursor->MacroModeClose();
310                         updateLocal(bv);
311                 }
312                 delete mathcursor;
313         }
314         mathcursor = 0;
315         bv->updateInset(this, false);
316 }
317
318
319 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
320 {
321         mathcursor->GetPos(x, y);
322         x -= par_->xo();
323         y -= par_->yo();
324 }
325
326
327 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
328 {
329         if (!mathcursor)
330                 return;
331
332         if (isCursorVisible())
333                 bv->hideLockedInsetCursor();
334         else {
335                 int x;
336                 int y;
337                 mathcursor->GetPos(x, y);
338                 //x -= par_->xo();
339                 y -= par_->yo();
340
341                 LyXFont   font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
342                 int const asc  = lyxfont::maxAscent(font);
343                 int const desc = lyxfont::maxDescent(font);
344
345                 bv->showLockedInsetCursor(x, y, asc, desc);
346         }
347
348         toggleCursorVisible();
349 }
350
351
352 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
353 {
354         if (!isCursorVisible()) {
355                 if (mathcursor) {
356                         int x;
357                         int y;
358                         mathcursor->GetPos(x, y);
359                         x -= par_->xo();
360                         y -= par_->yo();
361                         LyXFont font   = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
362                         int const asc  = lyxfont::maxAscent(font);
363                         int const desc = lyxfont::maxDescent(font);
364                         bv->fitLockedInsetCursor(x, y, asc, desc);
365                 }
366                 toggleInsetCursor(bv);
367         }
368 }
369
370
371 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
372 {
373         if (isCursorVisible())
374                 toggleInsetCursor(bv);
375 }
376
377
378 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
379 {
380         if (!mathcursor)
381                 return;
382
383         bv->updateInset(this, false);
384 }
385
386
387 vector<string> const InsetFormulaBase::getLabelList() const
388 {
389   return std::vector<string>();
390 }
391
392
393 void InsetFormulaBase::updateLocal(BufferView * bv)
394 {
395         par_->Metrics(LM_ST_TEXT);
396         bv->updateInset(this, true);
397 }
398
399
400 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
401                                           int x, int y, int /*button*/)
402 {
403         if (mathcursor) {
404                 hideInsetCursor(bv);
405                 x += par_->xo();
406                 y += par_->yo();
407                 mathcursor->SetPos(x, y);
408                 showInsetCursor(bv);
409                 if (sel_flag) {
410                         sel_flag = false;
411                         sel_x = 0;
412                         sel_y = 0;
413                 }
414                 bv->updateInset(this, false);
415         }
416 }
417
418
419 void InsetFormulaBase::insetButtonPress(BufferView * bv,
420                                         int x, int y, int /*button*/)
421 {
422         sel_flag = false;
423         sel_x = x;
424         sel_y = y;
425         if (mathcursor && mathcursor->Selection()) {
426                 mathcursor->SelClear();
427                 bv->updateInset(this, false);
428         }
429 }
430
431
432 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
433                                          int x, int y, int /*button*/)
434 {
435         if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
436                 sel_flag = true;
437                 hideInsetCursor(bv);
438                 mathcursor->SetPos(sel_x + par_->xo(), sel_y + par_->yo());
439                 mathcursor->SelStart();
440                 showInsetCursor(bv);
441                 mathcursor->GetPos(sel_x, sel_y);
442         } else if (sel_flag) {
443                 hideInsetCursor(bv);
444                 x += par_->xo();
445                 y += par_->yo();
446                 mathcursor->SetPos(x, y);
447                 showInsetCursor(bv);
448                 mathcursor->GetPos(x, y);
449                 if (sel_x != x || sel_y != y)
450                         bv->updateInset(this, false);
451                 sel_x = x;
452                 sel_y = y;
453         }
454 }
455
456
457 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
458 {
459         lyxerr[Debug::MATHED]
460                 << "Used InsetFormulaBase::InsetKeyPress." << endl;
461 }
462
463
464
465 UpdatableInset::RESULT
466 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
467                             string const & arg)
468 {
469         //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
470         //      << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
471         //   extern char *dispatch_result;
472
473         if (!mathcursor) 
474                 return UNDISPATCHED;
475
476         MathTextCodes varcode = LM_TC_MIN;
477         bool was_macro = mathcursor->InMacroMode();
478         bool sel = false;
479         bool space_on = false;
480         bool was_selection = mathcursor->Selection();
481         RESULT result = DISPATCHED;
482         static MathSpaceInset * sp = 0;
483
484         hideInsetCursor(bv);
485
486         if (mathcursor->getLastCode() == LM_TC_TEX)
487                 varcode = LM_TC_TEX;
488
489         mathcursor->normalize();
490
491         switch (action) {
492
493                 // --- Cursor Movements ---------------------------------------------
494
495         case LFUN_RIGHTSEL:
496                 sel = true; // fall through...
497
498         case LFUN_RIGHT:
499                 result = DISPATCH_RESULT(mathcursor->Right(sel));
500                 updateLocal(bv);
501                 break;
502
503
504         case LFUN_LEFTSEL:
505                 sel = true; // fall through
506
507         case LFUN_LEFT:
508                 result = DISPATCH_RESULT(mathcursor->Left(sel));
509                 updateLocal(bv);
510                 break;
511
512
513         case LFUN_UPSEL:
514                 sel = true;
515
516         case LFUN_UP:
517                 result = DISPATCH_RESULT(mathcursor->Up(sel));
518                 updateLocal(bv);
519                 break;
520
521
522         case LFUN_DOWNSEL:
523                 sel = true;
524
525         case LFUN_DOWN:
526                 result = DISPATCH_RESULT(mathcursor->Down(sel));
527                 updateLocal(bv);
528                 break;
529
530         case LFUN_HOME:
531                 mathcursor->Home();
532                 updateLocal(bv);
533                 break;
534
535         case LFUN_END:
536                 mathcursor->End();
537                 updateLocal(bv);
538                 break;
539
540         case LFUN_DELETE_LINE_FORWARD:
541                 bv->lockedInsetStoreUndo(Undo::DELETE);
542                 mathcursor->DelLine();
543                 updateLocal(bv);
544                 break;
545
546         case LFUN_TAB:
547                 mathcursor->idxNext();
548                 updateLocal(bv);
549                 break;
550
551         case LFUN_SHIFT_TAB:
552                 mathcursor->idxPrev();
553                 updateLocal(bv);
554                 break;
555
556         case LFUN_TABINSERT:
557                 bv->lockedInsetStoreUndo(Undo::EDIT);
558                 mathcursor->splitCell();
559                 updateLocal(bv);
560                 break;
561
562         case LFUN_BACKSPACE:
563                 if (!mathcursor->InMacroMode() && mathcursor->pos() == 0) {
564                         bv->lockedInsetStoreUndo(Undo::DELETE);
565                         mathcursor->pullArg();
566                         bv->updateInset(this, true);
567                         break;
568                 }
569                 if (!mathcursor->Left())
570                         break;
571                 // fall through...
572
573         case LFUN_DELETE:
574                 bv->lockedInsetStoreUndo(Undo::DELETE);
575                 mathcursor->Delete();
576                 bv->updateInset(this, true);
577                 break;
578
579                 //    case LFUN_GETXY:
580                 //      sprintf(dispatch_buffer, "%d %d",);
581                 //      dispatch_result = dispatch_buffer;
582                 //      break;
583         case LFUN_SETXY:
584         {
585                 lyxerr << "LFUN_SETXY broken!\n";
586                 int x;
587                 int y;
588                 int x1;
589                 int y1;
590                 istringstream is(arg.c_str());
591                 is >> x >> y;
592                 par_->GetXY(x1, y1);
593                 mathcursor->SetPos(x1 + x, y1 + y);
594                 updateLocal(bv);
595         }
596         break;
597
598                 // cursor selection ---------------------------- 
599
600         case LFUN_PASTE:
601                 if (was_macro)
602                         mathcursor->MacroModeClose();
603                 bv->lockedInsetStoreUndo(Undo::INSERT);
604                 mathcursor->SelPaste();
605                 updateLocal(bv);
606                 break;
607
608         case LFUN_CUT:
609                 bv->lockedInsetStoreUndo(Undo::DELETE);
610                 mathcursor->SelCut();
611                 updateLocal(bv);
612                 break;
613
614         case LFUN_COPY:
615                 mathcursor->SelCopy();
616                 break;
617
618         case LFUN_HOMESEL:
619         case LFUN_ENDSEL:
620         case LFUN_WORDRIGHTSEL:
621         case LFUN_WORDLEFTSEL:
622                 break;
623
624                 // --- accented characters ------------------------------
625
626         case LFUN_UMLAUT:     mathcursor->setAccent(LM_ddot); break;
627         case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
628         case LFUN_GRAVE:      mathcursor->setAccent(LM_grave); break;
629         case LFUN_ACUTE:      mathcursor->setAccent(LM_acute); break;
630         case LFUN_TILDE:      mathcursor->setAccent(LM_tilde); break;
631         case LFUN_MACRON:     mathcursor->setAccent(LM_bar); break;
632         case LFUN_DOT:        mathcursor->setAccent(LM_dot); break;
633         case LFUN_CARON:      mathcursor->setAccent(LM_check); break;
634         case LFUN_BREVE:      mathcursor->setAccent(LM_breve); break;
635         case LFUN_VECTOR:     mathcursor->setAccent(LM_vec); break;
636
637                 // Greek mode
638         case LFUN_GREEK:
639                 if (!greek_kb_flag) {
640                         greek_kb_flag = 1;
641                         bv->owner()->message(_("Math greek mode on"));
642                 } else
643                         greek_kb_flag = 0;
644                 break;
645
646                 // Greek keyboard
647         case LFUN_GREEK_TOGGLE:
648                 greek_kb_flag = greek_kb_flag ? 0 : 2;
649                 if (greek_kb_flag)
650                         bv->owner()->message(_("Math greek keyboard on"));
651                 else
652                         bv->owner()->message(_("Math greek keyboard off"));
653                 break;
654
655                 //  Math fonts
656         case LFUN_BOLD:    handleFont(bv, LM_TC_BF); break;
657         case LFUN_SANS:    handleFont(bv, LM_TC_SF); break;
658         case LFUN_EMPH:    handleFont(bv, LM_TC_CAL); break;
659         case LFUN_ROMAN:   handleFont(bv, LM_TC_RM); break;
660         case LFUN_CODE:    handleFont(bv, LM_TC_TT); break;
661         case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
662
663         case LFUN_MATH_MODE:
664                 handleFont(bv, LM_TC_TEXTRM);
665                 //bv->owner()->message(_("math text mode toggled"));
666                 break;
667
668 #ifndef NO_LATEX
669         case LFUN_TEX:
670                 if (!mathcursor->Selection()) {
671                         mathcursor->handleFont(LM_TC_TEX);
672                         //bv->owner()->message(_("TeX mode toggled"));
673                 }
674                 break;
675 #endif
676
677         case LFUN_MATH_LIMITS:
678                 bv->lockedInsetStoreUndo(Undo::INSERT);
679                 if (mathcursor->toggleLimits())
680                         updateLocal(bv);
681                 break;
682
683         case LFUN_MATH_SIZE:
684                 if (!arg.empty()) {
685                         bv->lockedInsetStoreUndo(Undo::INSERT);
686                         latexkeys const * l = in_word_set(arg);
687                         mathcursor->SetSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
688                         updateLocal(bv);
689                 }
690                 break;
691
692         case LFUN_INSERT_MATH:
693                 if (!arg.empty()) {
694                         bv->lockedInsetStoreUndo(Undo::INSERT);
695                         mathcursor->Interpret(arg);
696                         updateLocal(bv);
697                 }
698                 break;
699
700         case LFUN_INSERT_MATRIX:
701                 if (mathcursor) {
702                         bv->lockedInsetStoreUndo(Undo::INSERT);
703                         int m = 1;
704                         int n = 1;
705                         string v_align;
706                         string h_align;
707                         istringstream is(arg.c_str());
708                         is >> m >> n >> v_align >> h_align;
709                         MathArrayInset * p = new MathArrayInset(m, n);
710                         p->valign(v_align[0]);
711                         p->halign(h_align);
712                         mathcursor->insert(p);
713                         updateLocal(bv);
714                 }
715                 break;
716
717         case LFUN_MATH_DELIM:
718         {
719                 bv->lockedInsetStoreUndo(Undo::INSERT);
720                 int ilt = '(';
721                 int irt = '.';
722                 static const string vdelim("(){}[]./|");
723                 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
724
725                 if (arg.empty())
726                         break;
727
728                 istringstream is(arg.c_str());
729                 string lt;
730                 string rt;
731                 is >> lt >> rt;
732                 lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
733                 lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
734
735                 if (lt.size() > 1) {
736                         latexkeys const * l = in_word_set(lt);
737                         if (l)
738                                 ilt = l->id;
739                 } else if (vdelim.find(lt[0]) != string::npos)
740                                 ilt = lt[0];
741
742                 if (rt.size() > 1) {
743                         latexkeys const * l = in_word_set(rt);
744                         if (l)
745                                 irt = l->id;
746                 } else if (vdelim.find(rt[0]) != string::npos)
747                                 irt = rt[0];
748
749                 if (mathcursor->selection) {
750                         MathDelimInset * p = new MathDelimInset(ilt, irt);
751                         MathArray ar;
752                         mathcursor->selArray(ar);
753                         lyxerr << "selarray: " << ar << "\n";
754                         p->cell(0) = ar; 
755                         mathcursor->insert(p);
756                 } else {
757                         mathcursor->insert(new MathDelimInset(ilt, irt));
758                 }
759                 updateLocal(bv);
760                 break;
761         }
762
763         case LFUN_PROTECTEDSPACE:
764                 bv->lockedInsetStoreUndo(Undo::INSERT);
765                 mathcursor->insert(new MathSpaceInset(1));
766                 space_on = true;
767                 updateLocal(bv);
768                 break;
769
770         case LFUN_UNDO:
771                 bv->owner()->message(_("Invalid action in math mode!"));
772                 break;
773
774
775         case LFUN_MATH_HALIGN:
776         {
777                 bv->lockedInsetStoreUndo(Undo::INSERT);
778                 lyxerr << "handling halign '" << arg << "'\n";
779                 int idx;
780                 MathArrayInset * p = matrixpar(idx);
781                 if (!p)
782                         break; 
783                 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
784                 updateLocal(bv);
785                 break;
786         }
787
788         case LFUN_MATH_VALIGN:
789         {
790                 bv->lockedInsetStoreUndo(Undo::INSERT);
791                 lyxerr << "handling valign '" << arg << "'\n";
792                 int idx;
793                 MathArrayInset * p = matrixpar(idx);
794                 if (!p)
795                         break; 
796                 p->valign(arg.size() ? arg[0] : 'c');
797                 updateLocal(bv);
798                 break;
799         }
800
801         case LFUN_MATH_ROW_INSERT:
802         {
803                 bv->lockedInsetStoreUndo(Undo::INSERT);
804                 int idx;
805                 MathArrayInset * p = matrixpar(idx);
806                 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
807                 if (!p)
808                         break; 
809                 p->addRow(p->row(idx));
810                 updateLocal(bv);
811                 break;
812         }
813
814         case LFUN_MATH_ROW_DELETE:
815         {
816                 bv->lockedInsetStoreUndo(Undo::INSERT);
817                 int idx;
818                 MathArrayInset * p = matrixpar(idx);
819                 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
820                 if (!p)
821                         break; 
822                 p->delRow(p->row(idx));
823                 updateLocal(bv);
824                 break;
825         }
826
827         case LFUN_MATH_COLUMN_INSERT:
828         {
829                 bv->lockedInsetStoreUndo(Undo::INSERT);
830                 int idx;
831                 MathArrayInset * p = matrixpar(idx);
832                 if (!p)
833                         break; 
834                 p->addCol(p->col(idx));
835                 updateLocal(bv);
836                 break;
837         }
838
839         case LFUN_MATH_COLUMN_DELETE:
840         {
841                 bv->lockedInsetStoreUndo(Undo::INSERT);
842                 int idx;
843                 MathArrayInset * p = matrixpar(idx);
844                 if (!p)
845                         break; 
846                 p->delCol(p->col(idx));
847                 updateLocal(bv);
848                 break;
849         }
850
851         case LFUN_EXEC_COMMAND:
852                 result = UNDISPATCHED;
853                 break;
854
855         default:
856                 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
857                         unsigned char c = arg[0];
858                         lyxerr << "char: '" << c << "'  int: " << int(c) << endl;
859                         //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);      
860                         lyxerr << "trans: '" << c << "'  int: " << int(c) << endl;
861                         bv->lockedInsetStoreUndo(Undo::INSERT);
862
863                         if (c == ' ' && mathcursor->getAccent() == LM_hat) {
864                                 c = '^';
865                                 mathcursor->setAccent(0);
866                         }
867
868                         if (c == 0) {      // Dead key, do nothing
869                                 //lyxerr << "deadkey" << endl;
870                                 break;
871                         }
872
873                         if (isalpha(c)) {
874                                 if (mathcursor->getLastCode() == LM_TC_TEX) {
875                                         mathcursor->MacroModeOpen();
876                                         mathcursor->clearLastCode();
877                                         varcode = LM_TC_MIN;
878                                 } else if (!varcode) {          
879                                         short f = mathcursor->getLastCode() ?
880                                                 mathcursor->getLastCode() :
881                                                 mathcursor->nextCode();
882                                         varcode = MathIsAlphaFont(f) ?
883                                                 static_cast<MathTextCodes>(f) :
884                                                 LM_TC_VAR;
885                                 }
886                                 
887                                 //           lyxerr << "Varcode << vardoce;
888                                 MathTextCodes char_code = varcode;
889                                 if (greek_kb_flag) {
890                                         char greek[26] =
891                                         {'A', 'B', 'X',  0 , 'E',  0 ,  0 , 'H', 'I',  0 ,
892                                          'K',  0 , 'M', 'N', 'O',  0 ,  0 , 'P',  0 , 'T',
893                                          'Y',  0,   0,   0,   0 , 'Z' };
894                                         
895                                         if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
896                                                 char_code = LM_TC_RM;
897                                                 c = greek[c - 'A'];
898                                         } else
899                                                 char_code = LM_TC_SYMB;
900                                 }
901                                 
902                                 mathcursor->insert(c, char_code);
903                                 
904                                 if (greek_kb_flag && char_code == LM_TC_RM )
905                                         mathcursor->setLastCode(LM_TC_VAR);
906                                 
907                                 varcode = LM_TC_MIN;
908                                 
909                                 if (greek_kb_flag < 2)
910                                         greek_kb_flag = 0;
911                                 
912                         } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
913                                 mathcursor->insert(c, LM_TC_TEX);
914                                 if (c == '{') {
915                                         mathcursor->insert('}', LM_TC_TEX);
916                                         mathcursor->Left();
917                                 }
918                                 mathcursor->clearLastCode();
919                                 //             varcode = LM_TC_MIN;
920                         } else if (c == '_' && varcode == LM_TC_TEX) {
921                                 mathcursor->insert(c, LM_TC_SPECIAL);
922                                 mathcursor->clearLastCode();
923                                 //             varcode = LM_TC_MIN;
924                         } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
925                                 mathcursor->MacroModeOpen();
926                                 mathcursor->clearLastCode();
927                                 mathcursor->insert(c, LM_TC_MIN);
928                         } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
929                                 MathTextCodes code = mathcursor->getLastCode();
930                                 if (code != LM_TC_TEXTRM)
931                                         code = LM_TC_CONST;
932                                 mathcursor->insert(c, code);
933                         } else if (strchr("+/-*<>=", c)) {
934                                 MathTextCodes code = mathcursor->getLastCode();
935                                 if (code != LM_TC_TEXTRM)
936                                         code = LM_TC_BOP;
937                                 mathcursor->insert(c, code);
938                         } else if (strchr(latex_special_chars, c) && c!= '_') {
939                                 MathTextCodes code = mathcursor->getLastCode();
940                                 if (code != LM_TC_TEXTRM)
941                                         code = LM_TC_SPECIAL;
942                                 mathcursor->insert(c, code);
943                         } else if (c == '_' || c == '^') {
944                                 char s[2];
945                                 s[0] = c;
946                                 s[1] = 0;
947                                 mathcursor->Interpret(s);
948                         } else if (c == ' ') {  
949                                 if (!varcode) { 
950                                         short f = (mathcursor->getLastCode()) ?
951                                                 mathcursor->getLastCode() :
952                                                 mathcursor->nextCode();
953                                         varcode = MathIsAlphaFont(f) ?
954                                                 static_cast<MathTextCodes>(f) :
955                                                 LM_TC_VAR;
956                                 }
957                                 
958                                 if (varcode == LM_TC_TEXTRM) {
959                                         mathcursor->insert(c, LM_TC_TEXTRM);
960                                 } else if (was_macro) {
961                                         mathcursor->MacroModeClose();
962                                 } else if (sp) {
963                                         int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
964                                         sp->SetSpace(isp);
965                                         space_on = true;
966                                 } else {
967                                         if (!mathcursor->pop())
968                                                 result = FINISHED;
969                                         mathcursor->plainRight();
970                                 }
971                         } else if (c == '\'' || c == '@') {
972                                 mathcursor->insert (c, LM_TC_VAR);
973                         } else if (c == '\\') {
974                                 if (was_macro)
975                                         mathcursor->MacroModeClose();
976                                 bv->owner()->message(_("TeX mode"));
977                                 mathcursor->setLastCode(LM_TC_TEX);
978                         }
979                         updateLocal(bv);
980                 } else if (action == LFUN_MATH_PANEL) {
981                         result = UNDISPATCHED;
982                 } else {
983                         lyxerr << "Closed by action " << action << endl;
984                         result =  FINISHED;
985                 }
986         }
987
988         if (mathcursor)
989                 mathcursor->normalize();
990
991         if (mathcursor && was_macro != mathcursor->InMacroMode()
992                                 && action >= 0
993                                 && action != LFUN_BACKSPACE) 
994                 updateLocal(bv);
995         
996         //if (mathcursor)
997         //              updateLocal(bv);
998
999         if (sp && !space_on)
1000                 sp = 0;
1001
1002         if (mathcursor && (mathcursor->Selection() || was_selection))
1003                 toggleInsetSelection(bv);
1004
1005         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
1006             result == UNDISPATCHED)
1007                 showInsetCursor(bv);
1008         else
1009                 bv->unlockInset(this);
1010
1011         return result;  // original version
1012 }
1013
1014
1015
1016 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
1017  * on greek mode */
1018 bool math_insert_greek(BufferView * bv, char c)
1019 {
1020         if (!bv->available())
1021                 return false;
1022
1023         if (!isalpha(c))
1024                 return false;
1025
1026         string tmp;
1027         tmp = c;
1028         if (!bv->theLockingInset() || bv->theLockingInset()->isTextInset()) {
1029                 int greek_kb_flag_save = greek_kb_flag;
1030                 InsetFormula * new_inset = new InsetFormula();
1031                 bv->beforeChange(bv->text);
1032                 if (!bv->insertInset(new_inset)) {
1033                         delete new_inset;
1034                         return false;
1035                 }
1036                 //Update(1);//BUG
1037                 new_inset->edit(bv, 0, 0, 0);
1038                 new_inset->localDispatch(bv, LFUN_SELFINSERT, tmp);
1039                 if (greek_kb_flag_save < 2) {
1040                         bv->unlockInset(new_inset); // bv->theLockingInset());
1041                         bv->text->cursorRight(bv, true);
1042                 }
1043         } else
1044                 if (bv->theLockingInset()->lyxCode() == Inset::MATH_CODE ||
1045                                 bv->theLockingInset()->lyxCode() == Inset::MATHMACRO_CODE)
1046                         static_cast<InsetFormula*>(bv->theLockingInset())->localDispatch(bv, LFUN_SELFINSERT, tmp);
1047                 else
1048                         lyxerr << "Math error: attempt to write on a wrong "
1049                                 "class of inset." << endl;
1050         return true;
1051 }
1052
1053
1054
1055 Inset::Code InsetFormulaBase::lyxCode() const
1056 {
1057         return Inset::MATH_CODE;
1058 }
1059
1060
1061 LyXFont const InsetFormulaBase::convertFont(LyXFont const & f) const
1062 {
1063         // We have already discussed what was here
1064         LyXFont font(f);
1065 #ifndef NO_LATEX
1066         font.setLatex(LyXFont::OFF);
1067 #endif
1068         return font;
1069 }
1070
1071 MathInset * InsetFormulaBase::par() const
1072 {
1073         return par_;
1074 }
1075
1076
1077 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
1078 {
1079         if (bv->available()) {
1080 // Feature "Read math inset from selection" disabled.
1081 //              // use selection if available..
1082 //              string sel;
1083 //              if (action == LFUN_MATH_IMPORT_SELECTION)
1084 //                      sel = "";
1085 //              else
1086 //                      sel = bv->getLyXText()->selectionAsString(bv->buffer());
1087
1088                         InsetFormula * f;
1089 //              if (sel.empty()) {
1090                                 f = new InsetFormula;
1091                                 openNewInset(bv, f);
1092                                 // don't do that also for LFUN_MATH_MODE unless you want end up with
1093                                 // always changing to mathrm when opening an inlined inset
1094                                 // -- I really hate "LyXfunc overloading"...
1095                                 if (display)
1096                                         f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
1097                                 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
1098 //              } else {
1099 //                      f = new InsetFormula(sel);
1100 //                      bv->getLyXText()->cutSelection(bv);
1101 //                      openNewInset(bv, f);
1102 //              }
1103         }
1104         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
1105 }
1106
1107 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
1108 {
1109         mathDispatchCreation(bv, arg, true);
1110 }
1111         
1112 void mathDispatchMathMode(BufferView * bv, string const & arg)
1113 {
1114         mathDispatchCreation(bv, arg, false);
1115 }
1116
1117 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
1118 {
1119         mathDispatchCreation(bv, arg, true);
1120 }
1121
1122 void mathDispatchMathMacro(BufferView * bv, string const & arg)
1123 {
1124         if (bv->available()) {
1125                 string s(arg);
1126                 if (s.empty())
1127                         bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
1128                 else {
1129                         string const s1 = token(s, ' ', 1);
1130                         int const na = s1.empty() ? 0 : lyx::atoi(s1);
1131                         openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1132                 }
1133         }
1134 }
1135
1136 void mathDispatchMathDelim(BufferView * bv, string const & arg)
1137 {          
1138         if (bv->available()) { 
1139                 if (openNewInset(bv, new InsetFormula))
1140                         bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1141         }
1142 }          
1143
1144
1145 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1146 {          
1147         if (bv->available()) { 
1148                 if (openNewInset(bv, new InsetFormula))
1149                         bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1150         }
1151 }          
1152
1153 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1154 {
1155         if (bv->available()) {
1156                 if (arg.size() && arg[0] == '\\') {
1157                         InsetFormula * f = new InsetFormula(arg);
1158                         openNewInset(bv, f);
1159                 } else {
1160                         return mathDispatchMathDisplay(bv, arg);
1161                 }
1162         }
1163 }
1164