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