]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
some support for matrix operations with maple ('M-x math-extern maple evalm')
[lyx.git] / src / mathed / formulabase.C
1 /*
2 *  File:        formulabase.C
3 *  Purpose:     Implementation of common parts of the LyX  math insets
4 *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 *  Created:     January 1996
6 *
7 *  Copyright: 1996-1998 Alejandro Aguilar Sierra
8 *
9 *  Version: 0.4, Lyx project.
10 *
11 *   You are free to use and modify this code under the terms of
12 *   the GNU General Public Licence version 2 or later.
13 */
14
15 #include <config.h>
16 #include <fstream>
17
18 #include "Lsstream.h"
19 #include "support/LAssert.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 "BufferView.h"
30 #include "lyxtext.h"
31 #include "lyxfunc.h"
32 #include "gettext.h"
33 #include "LaTeXFeatures.h"
34 #include "debug.h"
35 #include "math_support.h"
36 #include "support/lstrings.h"
37 #include "LyXView.h"
38 #include "Painter.h"
39 #include "font.h"
40 #include "math_arrayinset.h"
41 #include "math_spaceinset.h"
42 #include "math_macrotable.h"
43 #include "math_factory.h"
44 #include "support/lyxlib.h"
45 #include "undo_funcs.h"
46
47 using std::endl;
48 using std::ostream;
49 using std::vector;
50
51 extern char const * latex_mathenv[];
52 MathCursor        * mathcursor = 0;
53
54
55 namespace {
56
57
58 // local global
59 int sel_x;
60 int sel_y;
61 bool sel_flag;
62
63
64 void handleFont(BufferView * bv, MathTextCodes t) 
65 {
66         if (mathcursor->selection())
67                 bv->lockedInsetStoreUndo(Undo::EDIT);
68         mathcursor->handleFont(t);
69 }
70
71
72 void handleAccent(BufferView * bv, string const & name)
73 {
74         bv->lockedInsetStoreUndo(Undo::EDIT);
75         mathcursor->insert(createMathInset(name));
76 }
77
78
79 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
80 {
81         if (!bv->insertInset(new_inset)) {
82                 delete new_inset;
83                 return false;
84         }
85         new_inset->edit(bv, 0, 0, 0);
86         return true;
87 }
88
89
90 } // namespace anon
91
92
93
94 InsetFormulaBase::InsetFormulaBase()
95         : view_(0), font_(), xo_(0), yo_(0)
96 {
97         // This is needed as long the math parser is not re-entrant
98         MathMacroTable::builtinMacros();
99         //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
100 }
101
102
103 // Check if uses AMS macros
104 void InsetFormulaBase::validate(LaTeXFeatures &) const
105 {}
106
107
108 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const 
109 {
110         if (bv)
111                 view_ = bv;
112         font_ = f;
113         MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
114         par()->metrics(mi);
115 }
116
117
118 string const InsetFormulaBase::editMessage() const
119 {
120         return _("Math editor mode");
121 }
122
123
124 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
125 {
126         if (!bv->lockInset(this))
127                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
128
129         mathcursor = new MathCursor(this, x == 0);
130         metrics(bv);
131         // if that is removed, we won't get the magenta box when entering an
132         // inset for the first time
133         bv->updateInset(this, false);
134         sel_x = 0;
135         sel_y = 0;
136         sel_flag = false;
137 }
138
139
140 void InsetFormulaBase::edit(BufferView * bv, bool front)
141 {
142         // looks hackish but seems to work
143         edit(bv, front ? 0 : 1, 0, 0);
144 }
145
146
147 void InsetFormulaBase::insetUnlock(BufferView * bv)
148 {
149         if (mathcursor) {
150                 if (mathcursor->inMacroMode()) {
151                         mathcursor->macroModeClose();
152                         updateLocal(bv, true);
153                 }
154                 delete mathcursor;
155                 mathcursor = 0;
156         }
157         bv->updateInset(this, false);
158 }
159
160
161 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
162 {
163         mathcursor->getPos(x, y);
164         x += xo_;
165         y += yo_ - 3;
166         //lyxerr << "getCursorPos: " << x << " " << y << "\n";
167 }
168
169
170 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
171 {
172         if (!mathcursor)
173                 return;
174
175         if (isCursorVisible())
176                 bv->hideLockedInsetCursor();
177         else {
178                 metrics(bv);
179                 int x;
180                 int y;
181                 mathcursor->getPos(x, y);
182                 y -= 3;
183                 y -= yo_;
184                 int asc = 0;
185                 int des = 0;
186                 MathMetricsInfo mi(bv, font_, LM_ST_TEXT);
187                 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
188                 bv->showLockedInsetCursor(x, y, asc, des);
189                 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
190         }
191
192         toggleCursorVisible();
193 }
194
195
196 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
197 {
198         if (!isCursorVisible()) {
199                 if (mathcursor) {
200                         int x;
201                         int y;
202                         mathcursor->getPos(x, y);
203                         int asc = 0;
204                         int des = 0;
205                         MathMetricsInfo mi(bv, font_, LM_ST_TEXT);
206                         math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
207                         //bv->fitLockedInsetCursor(x, y, asc, des);
208                         //metrics(bv);
209                         //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
210                 }
211                 toggleInsetCursor(bv);
212         }
213 }
214
215
216 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
217 {
218         if (isCursorVisible())
219                 toggleInsetCursor(bv);
220 }
221
222
223 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
224 {
225         if (mathcursor)
226                 bv->updateInset(this, false);
227 }
228
229
230 vector<string> const InsetFormulaBase::getLabelList() const
231 {
232   return std::vector<string>();
233 }
234
235
236 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
237 {
238         metrics(bv);
239         bv->updateInset(this, dirty);
240 }
241
242
243 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
244                                           int x, int y, int /*button*/)
245 {
246         if (mathcursor) {
247                 hideInsetCursor(bv);
248                 mathcursor->setPos(x + xo_, y + yo_);
249                 //lyxerr << "insetButtonRelease: " << x + xo_ << " " << y + yo_ << "\n";
250                 showInsetCursor(bv);
251                 if (sel_flag) {
252                         sel_flag = false;
253                         sel_x = 0;
254                         sel_y = 0;
255                 }
256                 bv->updateInset(this, false);
257         }
258 }
259
260
261 void InsetFormulaBase::insetButtonPress(BufferView * bv,
262                                         int x, int y, int /*button*/)
263 {
264         sel_flag = false;
265         sel_x = x;
266         sel_y = y;
267         if (mathcursor && mathcursor->selection()) {
268                 mathcursor->selClear();
269                 bv->updateInset(this, false);
270         }
271 }
272
273
274 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
275                                          int x, int y, int /*button*/)
276 {
277         if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
278                 sel_flag = true;
279                 hideInsetCursor(bv);
280                 mathcursor->setPos(sel_x, sel_y);
281                 mathcursor->selStart();
282                 showInsetCursor(bv);
283                 mathcursor->getPos(sel_x, sel_y);
284         } else if (sel_flag) {
285                 hideInsetCursor(bv);
286                 mathcursor->setPos(x, y);
287                 showInsetCursor(bv);
288                 mathcursor->getPos(x, y);
289                 if (sel_x != x || sel_y != y)
290                         bv->updateInset(this, false);
291                 sel_x = x;
292                 sel_y = y;
293         }
294 }
295
296
297 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
298 {
299         lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
300 }
301
302
303 UpdatableInset::RESULT
304 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
305                             string const & arg)
306 {
307         //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
308         //      << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
309
310         if (!mathcursor) 
311                 return UNDISPATCHED;
312
313         if (mathcursor->asHyperActiveInset()) {
314                 lyxerr << " uurr.... getting dificult now\n";
315                 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
316         }
317
318         RESULT result      = DISPATCHED;
319         bool sel           = false;
320         bool was_macro     = mathcursor->inMacroMode();
321         bool was_selection = mathcursor->selection();
322
323         hideInsetCursor(bv);
324
325         mathcursor->normalize();
326
327         switch (action) {
328
329                 // --- Cursor Movements ---------------------------------------------
330
331         case LFUN_RIGHTSEL:
332                 sel = true; // fall through...
333
334         case LFUN_RIGHT:
335                 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
336                 updateLocal(bv, false);
337                 break;
338
339
340         case LFUN_LEFTSEL:
341                 sel = true; // fall through
342
343         case LFUN_LEFT:
344                 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
345                 updateLocal(bv, false);
346                 break;
347
348
349         case LFUN_UPSEL:
350                 sel = true;
351
352         case LFUN_UP:
353                 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
354                 updateLocal(bv, false);
355                 break;
356
357
358         case LFUN_DOWNSEL:
359                 sel = true;
360
361         case LFUN_DOWN:
362                 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
363                 updateLocal(bv, false);
364                 break;
365
366         case LFUN_HOMESEL:
367                 sel = true;
368
369         case LFUN_HOME:
370                 mathcursor->home(sel);
371                 updateLocal(bv, false);
372                 break;
373
374         case LFUN_ENDSEL:
375                 sel = true;
376
377         case LFUN_END:
378                 mathcursor->end(sel);
379                 updateLocal(bv, false);
380                 break;
381
382         case LFUN_DELETE_LINE_FORWARD:
383                 bv->lockedInsetStoreUndo(Undo::DELETE);
384                 mathcursor->delLine();
385                 updateLocal(bv, true);
386                 break;
387
388         case LFUN_TAB:
389                 mathcursor->idxNext();
390                 updateLocal(bv, false);
391                 break;
392
393         case LFUN_SHIFT_TAB:
394                 mathcursor->idxPrev();
395                 updateLocal(bv, false);
396                 break;
397
398         case LFUN_TABINSERT:
399                 bv->lockedInsetStoreUndo(Undo::EDIT);
400                 mathcursor->splitCell();
401                 updateLocal(bv, true);
402                 break;
403
404         case LFUN_DELETE_WORD_BACKWARD:
405         case LFUN_BACKSPACE:
406                 bv->lockedInsetStoreUndo(Undo::DELETE);
407                 mathcursor->backspace();
408                 bv->updateInset(this, true);
409                 break;
410
411         case LFUN_DELETE_WORD_FORWARD:
412         case LFUN_DELETE:
413                 bv->lockedInsetStoreUndo(Undo::DELETE);
414                 mathcursor->erase();
415                 bv->updateInset(this, true);
416                 break;
417
418                 //    case LFUN_GETXY:
419                 //      sprintf(dispatch_buffer, "%d %d",);
420                 //      dispatch_result = dispatch_buffer;
421                 //      break;
422         case LFUN_SETXY: {
423                 lyxerr << "LFUN_SETXY broken!\n";
424                 int x = 0;
425                 int y = 0;
426                 istringstream is(arg.c_str());
427                 is >> x >> y;
428                 mathcursor->setPos(x, y);
429                 updateLocal(bv, false);
430                 break;
431         }
432
433         case LFUN_PASTE:
434                 if (was_macro)
435                         mathcursor->macroModeClose();
436                 bv->lockedInsetStoreUndo(Undo::EDIT);
437                 mathcursor->selPaste();
438                 updateLocal(bv, true);
439                 break;
440
441         case LFUN_CUT:
442                 bv->lockedInsetStoreUndo(Undo::DELETE);
443                 mathcursor->selCut();
444                 updateLocal(bv, true);
445                 break;
446
447         case LFUN_COPY:
448                 mathcursor->selCopy();
449                 break;
450
451         case LFUN_WORDRIGHTSEL:
452         case LFUN_WORDLEFTSEL:
453                 break;
454
455                 // --- accented characters ------------------------------
456
457         case LFUN_UMLAUT:       handleAccent(bv, "ddot"); break;
458         case LFUN_CIRCUMFLEX:   handleAccent(bv, "hat"); break;
459         case LFUN_GRAVE:        handleAccent(bv, "grave"); break;
460         case LFUN_ACUTE:        handleAccent(bv, "acute"); break;
461         case LFUN_TILDE:        handleAccent(bv, "tilde"); break;
462         case LFUN_MACRON:       handleAccent(bv, "bar"); break;
463         case LFUN_DOT:          handleAccent(bv, "dot"); break;
464         case LFUN_CARON:        handleAccent(bv, "check"); break;
465         case LFUN_BREVE:        handleAccent(bv, "breve"); break;
466         case LFUN_VECTOR:       handleAccent(bv, "vec"); break;
467
468         //  Math fonts
469         case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
470         case LFUN_BOLD:         handleFont(bv, LM_TC_BF); break;
471         case LFUN_SANS:         handleFont(bv, LM_TC_SF); break;
472         case LFUN_EMPH:         handleFont(bv, LM_TC_CAL); break;
473         case LFUN_ROMAN:        handleFont(bv, LM_TC_RM); break;
474         case LFUN_CODE:         handleFont(bv, LM_TC_TT); break;
475         case LFUN_NOUN:         handleFont(bv, LM_TC_BB); break;
476         case LFUN_DEFAULT:      handleFont(bv, LM_TC_VAR); break;
477
478         case LFUN_GREEK: 
479                 handleFont(bv, LM_TC_GREEK1);
480                 if (arg.size())
481                         mathcursor->interpret(arg);
482                 break;
483
484         case LFUN_MATH_MODE:
485                 handleFont(bv, LM_TC_TEXTRM);
486                 //bv->owner()->message(_("math text mode toggled"));
487                 break;
488
489         case LFUN_MATH_LIMITS:
490                 bv->lockedInsetStoreUndo(Undo::EDIT);
491                 if (mathcursor->toggleLimits())
492                         updateLocal(bv, true);
493                 break;
494
495         case LFUN_MATH_SIZE:
496 #if 0
497                 if (!arg.empty()) {
498                         bv->lockedInsetStoreUndo(Undo::EDIT);
499                         mathcursor->setSize(arg);
500                         updateLocal(bv, true);
501                 }
502 #endif
503                 break;
504
505         case LFUN_INSERT_MATRIX:
506                 if (!arg.empty()) {
507                         bv->lockedInsetStoreUndo(Undo::EDIT);
508                         mathcursor->interpret("matrix " + arg);
509                         updateLocal(bv, true);
510                 }
511                 break;
512
513         case LFUN_MATH_SPACE:
514         {
515                 bv->lockedInsetStoreUndo(Undo::EDIT);
516                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
517                 updateLocal(bv, true);
518                 break;
519         }
520
521         case LFUN_MATH_DELIM:
522         {
523                 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
524                 string ls;
525                 string rs;
526                 istringstream is(arg.c_str());
527                 is >> ls >> rs;
528                 if (!is) {
529                         lyxerr << "can't parse delimeters from '" << arg << "'\n";
530                         break;
531                 }
532                 bv->lockedInsetStoreUndo(Undo::EDIT);
533                 mathcursor->handleDelim(ls, rs);
534                 updateLocal(bv, true);
535                 break;
536         }
537
538         case LFUN_PROTECTEDSPACE:
539                 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
540                 bv->lockedInsetStoreUndo(Undo::EDIT);
541                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
542                 updateLocal(bv, true);
543                 break;
544
545         case LFUN_UNDO:
546                 bv->owner()->message(_("Invalid action in math mode!"));
547                 break;
548
549
550         case LFUN_MATH_HALIGN:
551         case LFUN_MATH_VALIGN:
552         case LFUN_MATH_ROW_INSERT:
553         case LFUN_MATH_ROW_DELETE:
554         case LFUN_MATH_COLUMN_INSERT:
555         case LFUN_MATH_COLUMN_DELETE:
556         {
557                 MathInset::idx_type idx = 0;
558                 MathArrayInset * p = mathcursor ? mathcursor->enclosingArray(idx) : 0;
559                 if (p) {
560                         bv->lockedInsetStoreUndo(Undo::EDIT);
561                         char al = arg.size() ? arg[0] : 'c';
562                         switch (action) {
563                                 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
564                                 case LFUN_MATH_VALIGN: p->valign(al); break;
565                                 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
566                                 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
567                                 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
568                                 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
569                                 default: ;
570                         }
571                         updateLocal(bv, true);
572                 }
573                 break;
574         }
575
576         case LFUN_EXEC_COMMAND:
577                 result = UNDISPATCHED;
578                 break;
579
580         case LFUN_BREAKPARAGRAPH:
581         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
582                 //lyxerr << "LFUN ignored\n";
583                 break;
584
585         case -1:
586         case LFUN_INSERT_MATH:
587         case LFUN_SELFINSERT:
588                 if (!arg.empty()) {
589                         bv->lockedInsetStoreUndo(Undo::EDIT);
590                         mathcursor->interpret(arg);
591                         updateLocal(bv, true);
592                 }
593                 break;
594
595         case LFUN_MATH_PANEL:
596                 result = UNDISPATCHED;
597                 break;
598
599         default:
600                 result = UNDISPATCHED;
601         }
602
603         lyx::Assert(mathcursor);
604         //mathcursor->normalize();
605
606         if (//was_macro != mathcursor->inMacroMode() &&
607                                 action >= 0 && action != LFUN_BACKSPACE) 
608                 updateLocal(bv, true);
609         
610         if (mathcursor->selection() || was_selection)
611                 toggleInsetSelection(bv);
612
613         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
614             result == UNDISPATCHED)
615                 showInsetCursor(bv);
616         else
617                 bv->unlockInset(this);
618
619         return result;  // original version
620 }
621
622
623 Inset::Code InsetFormulaBase::lyxCode() const
624 {
625         return Inset::MATH_CODE;
626 }
627
628
629 int InsetFormulaBase::upperY() const
630 {
631         return yo_ - ascent(view_, font_);
632 }
633
634
635 int InsetFormulaBase::lowerY() const
636 {
637         return yo_ + descent(view_, font_);
638 }
639
640
641 /////////////////////////////////////////////////////////////////////
642
643
644 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
645 {
646         if (bv->available()) {
647                 // use selection if available..
648                 //string sel;
649                 //if (action == LFUN_MATH_IMPORT_SELECTION)
650                 //      sel = "";
651                 //else
652
653                 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
654
655                 InsetFormulaBase * f;
656                 if (sel.empty()) {
657                         f = new InsetFormula;
658                         if (openNewInset(bv, f)) {
659                                 // don't do that also for LFUN_MATH_MODE unless you want end up with
660                                 // always changing to mathrm when opening an inlined inset
661                                 // -- I really hate "LyXfunc overloading"...
662                                 if (display)
663                                         f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
664                                 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
665                         }
666                 } else {
667                         // create a macro if we see "\\newcommand" somewhere, and an ordinary
668                         // formula otherwise
669                         if (sel.find("\\newcommand") == string::npos) 
670                                 f = new InsetFormula(sel);
671                         else
672                                 f = new InsetFormulaMacro(sel);
673                         bv->getLyXText()->cutSelection(bv);
674                         openNewInset(bv, f);
675                 }
676         }
677         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
678 }
679
680
681 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
682 {
683         mathDispatchCreation(bv, arg, true);
684 }
685
686         
687 void mathDispatchMathMode(BufferView * bv, string const & arg)
688 {
689         mathDispatchCreation(bv, arg, false);
690 }
691
692
693 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
694 {
695         mathDispatchCreation(bv, arg, true);
696 }
697
698
699 void mathDispatchMathMacro(BufferView * bv, string const & arg)
700 {
701         if (bv->available()) {
702                 if (arg.empty())
703                         bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
704                 else {
705                         string s(arg);
706                         string const s1 = token(s, ' ', 1);
707                         int const na = s1.empty() ? 0 : lyx::atoi(s1);
708                         openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
709                 }
710         }
711 }
712
713
714 void mathDispatchMathDelim(BufferView * bv, string const & arg)
715 {          
716         if (bv->available()) { 
717                 if (openNewInset(bv, new InsetFormula))
718                         bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
719         }
720 }          
721
722
723 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
724 {          
725         if (bv->available()) { 
726                 if (openNewInset(bv, new InsetFormula))
727                         bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
728         }
729 }          
730
731
732 void mathDispatchInsertMath(BufferView * bv, string const & arg)
733 {
734         if (bv->available()) {
735                 if (arg.size() && arg[0] == '\\') {
736                         InsetFormula * f = new InsetFormula(arg);
737                         if (!bv->insertInset(f))
738                                 delete f;
739                 } else
740                         mathDispatchMathMode(bv, arg);
741         }
742 }
743
744
745 void mathDispatchGreek(BufferView * bv, string const & arg)
746 {          
747         if (bv->available()) { 
748                 InsetFormula * f = new InsetFormula;
749                 if (openNewInset(bv, f)) {
750                         bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
751                         bv->unlockInset(f);
752                 }
753         }
754 }          
755
756