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