]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
Forgot to save changed file ;)
[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 "BufferView.h"
29 #include "lyxtext.h"
30 #include "lyxfunc.h"
31 #include "gettext.h"
32 #include "LaTeXFeatures.h"
33 #include "debug.h"
34 #include "math_support.h"
35 #include "support/lstrings.h"
36 #include "LyXView.h"
37 #include "Painter.h"
38 #include "font.h"
39 #include "Lsstream.h"
40 #include "math_arrayinset.h"
41 #include "math_charinset.h"
42 #include "math_cursor.h"
43 #include "math_factory.h"
44 #include "math_hullinset.h"
45 #include "math_iterator.h"
46 #include "math_macrotable.h"
47 #include "math_parser.h"
48 #include "math_pos.h"
49 #include "math_spaceinset.h"
50 #include "undo_funcs.h"
51 #include "textpainter.h"
52 #include "frontends/Dialogs.h"
53 #include "intl.h"
54
55 using std::endl;
56 using std::ostream;
57 using std::vector;
58
59 MathCursor * mathcursor = 0;
60
61
62 namespace {
63
64
65 // local global
66 int first_x;
67 int first_y;
68
69
70 void handleFont(BufferView * bv, string const & arg, MathTextCodes t)
71 {
72         if (mathcursor->selection())
73                 bv->lockedInsetStoreUndo(Undo::EDIT);
74         mathcursor->handleFont(t);
75         for (string::const_iterator it = arg.begin(); it != arg.end(); ++it)
76                 mathcursor->insert(*it);
77 }
78
79
80 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
81 {
82         if (!bv->insertInset(new_inset)) {
83                 delete new_inset;
84                 return false;
85         }
86         new_inset->edit(bv, 0, 0, 0);
87         return true;
88 }
89
90
91 } // namespace anon
92
93
94
95 InsetFormulaBase::InsetFormulaBase()
96         : view_(0), font_(), xo_(0), yo_(0)
97 {
98         // This is needed as long the math parser is not re-entrant
99         MathMacroTable::builtinMacros();
100         //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
101         //lyxerr << "sizeof(MathMetricsInfo): " << sizeof(MathMetricsInfo) << "\n";
102         //lyxerr << "sizeof(MathCharInset): " << sizeof(MathCharInset) << "\n";
103 }
104
105
106 // simply scrap this function if you want
107 void InsetFormulaBase::mutateToText()
108 {
109 #if 0
110         // translate to latex
111         ostringstream os;
112         latex(NULL, os, false, false);
113         string str = os.str();
114
115         // insert this text
116         LyXText * lt = view_->getLyXText();
117         string::const_iterator cit = str.begin();
118         string::const_iterator end = str.end();
119         for (; cit != end; ++cit)
120                 view_->owner()->getIntl()->getTrans().TranslateAndInsert(*cit, lt);
121
122         // remove ourselves
123         //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
124 #endif
125 }
126
127
128 // Check if uses AMS macros
129 void InsetFormulaBase::validate(LaTeXFeatures &) const
130 {}
131
132
133 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
134 {
135         font_ = f;
136         metrics(bv);
137 }
138
139
140 void InsetFormulaBase::metrics(BufferView * bv) const
141 {
142         if (bv)
143                 view_ = bv;
144         MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
145         par()->metrics(mi);
146 }
147
148
149 string const InsetFormulaBase::editMessage() const
150 {
151         return _("Math editor mode");
152 }
153
154
155 void InsetFormulaBase::edit(BufferView * bv, int x, int y, unsigned int)
156 {
157         if (!bv->lockInset(this))
158                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
159         delete mathcursor;
160         mathcursor = new MathCursor(this, true);
161         metrics(bv);
162         mathcursor->setPos(x, y);
163         //lyxerr << "setting pos to " << x << "," << y << "\n";
164         
165         // if that is removed, we won't get the magenta box when entering an
166         // inset for the first time
167         bv->updateInset(this, false);
168 }
169
170
171 void InsetFormulaBase::edit(BufferView * bv, bool front)
172 {
173         if (!bv->lockInset(this))
174                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
175         delete mathcursor;
176         mathcursor = new MathCursor(this, front);
177         metrics(bv);
178         bv->updateInset(this, false);
179 }
180
181
182 void InsetFormulaBase::insetUnlock(BufferView * bv)
183 {
184         if (mathcursor) {
185                 if (mathcursor->inMacroMode()) {
186                         mathcursor->macroModeClose();
187                         updateLocal(bv, true);
188                 }
189                 delete mathcursor;
190                 mathcursor = 0;
191         }
192         bv->updateInset(this, false);
193 }
194
195
196 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
197 {
198         mathcursor->getPos(x, y);
199         //x -= xo_;
200         y -= yo_;
201         //lyxerr << "getCursorPos: " << x << " " << y << "\n";
202 }
203
204
205 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
206 {
207         if (!mathcursor)
208                 return;
209
210         if (isCursorVisible())
211                 bv->hideLockedInsetCursor();
212         else {
213                 metrics(bv);
214                 int x;
215                 int y;
216                 mathcursor->getPos(x, y);
217                 y -= yo_;
218                 int asc = 0;
219                 int des = 0;
220                 math_font_max_dim(font_, asc, des);
221                 bv->showLockedInsetCursor(x, y, asc, des);
222                 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
223         }
224
225         toggleCursorVisible();
226 }
227
228
229 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
230 {
231         if (isCursorVisible())
232                 return;
233         if (mathcursor) {
234                 metrics(bv);
235                 int x;
236                 int y;
237                 mathcursor->getPos(x, y);
238                 y -= yo_;
239                 int asc = 0;
240                 int des = 0;
241                 math_font_max_dim(font_, asc, des);
242                 bv->fitLockedInsetCursor(x, y, asc, des);
243                 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
244         }
245         toggleInsetCursor(bv);
246 }
247
248
249 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
250 {
251         if (isCursorVisible())
252                 toggleInsetCursor(bv);
253 }
254
255
256 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
257 {
258         if (mathcursor)
259                 bv->updateInset(this, false);
260 }
261
262
263 vector<string> const InsetFormulaBase::getLabelList() const
264 {
265   return vector<string>();
266 }
267
268
269 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
270 {
271         metrics(bv);
272         bv->updateInset(this, dirty);
273 }
274
275
276 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
277                                           int /*x*/, int /*y*/, int button)
278 {
279         //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
280
281         if (!mathcursor)
282                 return false;
283         hideInsetCursor(bv);
284         showInsetCursor(bv);
285         bv->updateInset(this, false);
286
287         if (button == 3) {
288                 // launch math panel for right mouse button
289                 bv->owner()->getDialogs()->showMathPanel();
290                 return true;
291         }
292         return false;
293 }
294
295
296 void InsetFormulaBase::insetButtonPress(BufferView * bv,
297                                         int x, int y, int button)
298 {
299         //lyxerr << "insetButtonPress: "
300         //      << x << " " << y << " but: " << button << "\n";
301
302         switch (button) {
303                 default:
304                 case 1:
305                         // left click
306                         delete mathcursor;
307                         mathcursor = new MathCursor(this, x == 0);
308                         metrics(bv);
309                         first_x = x;
310                         first_y = y;
311                         mathcursor->selClear();
312                         mathcursor->setPos(x + xo_, y + yo_);
313                         break;
314 /*
315                 case 2:
316                         lyxerr << "insetButtonPress: 2\n";
317                         // insert stuff
318                         if (mathcursor) {
319                                 bv->lockedInsetStoreUndo(Undo::EDIT);
320                                 MathArray ar;
321                                 mathcursor->selGet(ar);
322                                 mathcursor->setPos(x + xo_, y + yo_);
323                                 string sel =
324                                         bv->getLyXText()->selectionAsString(bv->buffer(), false);
325                                 mathed_parse_cell(ar, sel);
326                                 mathcursor->insert(ar);
327                         }
328                         break;
329 */
330                 case 3:
331                         // launch math panel for right mouse button
332                         bv->owner()->getDialogs()->showMathPanel();
333                         break;
334         }
335         bv->updateInset(this, false);
336 }
337
338
339 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
340                                          int x, int y, int /*button*/)
341 {
342         if (!mathcursor)
343                 return;
344
345         if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
346                 //lyxerr << "insetMotionNotify: ignored\n";
347                 return;
348         }
349         first_x = x;
350         first_y = y;
351
352         if (!mathcursor->selection())
353                 mathcursor->selStart();
354
355         //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
356         //      << ' ' << button << "\n";
357         hideInsetCursor(bv);
358         mathcursor->setPos(x + xo_, y + yo_);
359         showInsetCursor(bv);
360         bv->updateInset(this, false);
361 }
362
363
364 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
365 {
366         lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
367 }
368
369
370 UpdatableInset::RESULT
371 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
372                             string const & arg)
373 {
374         //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
375         //      << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
376
377         if (!mathcursor)
378                 return UNDISPATCHED;
379
380         RESULT result      = DISPATCHED;
381         bool sel           = false;
382         bool was_macro     = mathcursor->inMacroMode();
383         bool was_selection = mathcursor->selection();
384
385         hideInsetCursor(bv);
386
387         mathcursor->normalize();
388         mathcursor->touch();
389
390         switch (action) {
391
392                 // --- Cursor Movements ---------------------------------------------
393
394         case LFUN_RIGHTSEL:
395                 sel = true; // fall through...
396
397         case LFUN_RIGHT:
398                 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
399                 //lyxerr << "calling scroll 20\n";
400                 //scroll(bv, 20);
401                 updateLocal(bv, false);
402                 // write something to the minibuffer
403                 //bv->owner()->message(mathcursor->info());
404                 break;
405
406
407         case LFUN_LEFTSEL:
408                 sel = true; // fall through
409
410         case LFUN_LEFT:
411                 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
412                 updateLocal(bv, false);
413                 break;
414
415
416         case LFUN_UPSEL:
417                 sel = true;
418
419         case LFUN_UP:
420                 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
421                 updateLocal(bv, false);
422                 break;
423
424
425         case LFUN_DOWNSEL:
426                 sel = true;
427
428         case LFUN_DOWN:
429                 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
430                 updateLocal(bv, false);
431                 break;
432
433         case LFUN_HOMESEL:
434                 sel = true;
435
436         case LFUN_HOME:
437                 mathcursor->home(sel);
438                 updateLocal(bv, false);
439                 break;
440
441         case LFUN_ENDSEL:
442                 sel = true;
443
444         case LFUN_END:
445                 mathcursor->end(sel);
446                 updateLocal(bv, false);
447                 break;
448
449         case LFUN_DELETE_LINE_FORWARD:
450                 bv->lockedInsetStoreUndo(Undo::DELETE);
451                 mathcursor->delLine();
452                 updateLocal(bv, true);
453                 break;
454
455         case LFUN_TAB:
456                 mathcursor->idxNext();
457                 updateLocal(bv, false);
458                 break;
459
460         case LFUN_SHIFT_TAB:
461                 mathcursor->idxPrev();
462                 updateLocal(bv, false);
463                 break;
464
465         case LFUN_TABINSERT:
466                 bv->lockedInsetStoreUndo(Undo::EDIT);
467                 mathcursor->splitCell();
468                 updateLocal(bv, true);
469                 break;
470
471         case LFUN_DELETE_WORD_BACKWARD:
472         case LFUN_BACKSPACE:
473                 bv->lockedInsetStoreUndo(Undo::DELETE);
474                 mathcursor->backspace();
475                 bv->updateInset(this, true);
476                 break;
477
478         case LFUN_DELETE_WORD_FORWARD:
479         case LFUN_DELETE:
480                 bv->lockedInsetStoreUndo(Undo::DELETE);
481                 mathcursor->erase();
482                 bv->updateInset(this, true);
483                 break;
484
485         //    case LFUN_GETXY:
486         //      sprintf(dispatch_buffer, "%d %d",);
487         //      dispatch_result = dispatch_buffer;
488         //      break;
489         case LFUN_SETXY: {
490                 lyxerr << "LFUN_SETXY broken!\n";
491                 int x = 0;
492                 int y = 0;
493                 istringstream is(arg.c_str());
494                 is >> x >> y;
495                 mathcursor->setPos(x, y);
496                 updateLocal(bv, false);
497                 break;
498         }
499
500         case LFUN_PASTE:
501                 if (was_macro)
502                         mathcursor->macroModeClose();
503                 bv->lockedInsetStoreUndo(Undo::EDIT);
504                 mathcursor->selPaste();
505                 updateLocal(bv, true);
506                 break;
507
508         case LFUN_CUT:
509                 bv->lockedInsetStoreUndo(Undo::DELETE);
510                 mathcursor->selCut();
511                 updateLocal(bv, true);
512                 break;
513
514         case LFUN_COPY:
515                 mathcursor->selCopy();
516                 break;
517
518         case LFUN_WORDRIGHTSEL:
519         case LFUN_WORDLEFTSEL:
520                 break;
521
522         // Special casing for superscript in case of LyX handling
523         // dead-keys:
524         case LFUN_CIRCUMFLEX:
525                 if (arg.empty()) {
526                         // do superscript if LyX handles
527                         // deadkeys
528                         bv->lockedInsetStoreUndo(Undo::EDIT);
529                         mathcursor->script(true);
530                         updateLocal(bv, true);
531                 }
532                 break;
533         case LFUN_UMLAUT:
534         case LFUN_ACUTE:
535         case LFUN_GRAVE:
536         case LFUN_BREVE:
537         case LFUN_DOT:
538         case LFUN_MACRON:
539         case LFUN_CARON:
540         case LFUN_TILDE:
541         case LFUN_CEDILLA:
542         case LFUN_CIRCLE:
543         case LFUN_UNDERDOT:
544         case LFUN_TIE:
545         case LFUN_OGONEK:
546         case LFUN_HUNG_UMLAUT:
547                 break;
548
549         //  Math fonts
550         case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
551         case LFUN_BOLD:         handleFont(bv, arg, LM_TC_BF); break;
552         case LFUN_SANS:         handleFont(bv, arg, LM_TC_SF); break;
553         case LFUN_EMPH:         handleFont(bv, arg, LM_TC_CAL); break;
554         case LFUN_ROMAN:        handleFont(bv, arg, LM_TC_RM); break;
555         case LFUN_CODE:         handleFont(bv, arg, LM_TC_TT); break;
556         case LFUN_FRAK:         handleFont(bv, arg, LM_TC_EUFRAK); break;
557         case LFUN_ITAL:         handleFont(bv, arg, LM_TC_IT); break;
558         case LFUN_NOUN:         handleFont(bv, arg, LM_TC_BB); break;
559         case LFUN_DEFAULT:      handleFont(bv, arg, LM_TC_VAR); break;
560         case LFUN_FREE:         handleFont(bv, arg, LM_TC_TEXTRM); break;
561
562         case LFUN_GREEK:
563                 handleFont(bv, arg, LM_TC_GREEK1);
564                 if (arg.size())
565                         mathcursor->interpret(arg);
566                 break;
567
568         case LFUN_MATH_MODE:
569 #if 1
570                 handleFont(bv, arg, LM_TC_TEXTRM);
571 #endif
572
573 #if 0
574                 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
575                 updateLocal(bv, true);
576 #endif
577
578                 //bv->owner()->message(_("math text mode toggled"));
579                 break;
580
581         case LFUN_MATH_LIMITS:
582                 bv->lockedInsetStoreUndo(Undo::EDIT);
583                 if (mathcursor->toggleLimits())
584                         updateLocal(bv, true);
585                 break;
586
587         case LFUN_MATH_SIZE:
588 #if 0
589                 if (!arg.empty()) {
590                         bv->lockedInsetStoreUndo(Undo::EDIT);
591                         mathcursor->setSize(arg);
592                         updateLocal(bv, true);
593                 }
594 #endif
595                 break;
596
597         case LFUN_INSERT_MATRIX:
598                 if (!arg.empty()) {
599                         bv->lockedInsetStoreUndo(Undo::EDIT);
600                         mathcursor->interpret("matrix " + arg);
601                         updateLocal(bv, true);
602                 }
603                 break;
604
605         case LFUN_MATH_SPACE:
606         {
607                 bv->lockedInsetStoreUndo(Undo::EDIT);
608                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
609                 updateLocal(bv, true);
610                 break;
611         }
612
613         case LFUN_SUPERSCRIPT:
614         case LFUN_SUBSCRIPT:
615         {
616                 bv->lockedInsetStoreUndo(Undo::EDIT);
617                 mathcursor->script((action == LFUN_SUPERSCRIPT));
618                 updateLocal(bv, true);
619                 break;
620         }
621
622         case LFUN_MATH_DELIM:
623         {
624                 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
625                 string ls;
626                 string rs = split(arg, ls, ' ');
627                 // Reasonable default values
628                 if (ls.empty())
629                         ls = '(';
630                 if (rs.empty())
631                         rs = ')';
632                 
633                 bv->lockedInsetStoreUndo(Undo::EDIT);
634                 mathcursor->handleDelim(ls, rs);
635                 updateLocal(bv, true);
636                 break;
637         }
638
639         case LFUN_PROTECTEDSPACE:
640                 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
641                 bv->lockedInsetStoreUndo(Undo::EDIT);
642                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
643                 updateLocal(bv, true);
644                 break;
645
646         case LFUN_UNDO:
647                 bv->owner()->message(_("Invalid action in math mode!"));
648                 break;
649
650
651         case LFUN_MATH_HALIGN:
652         case LFUN_MATH_VALIGN:
653         case LFUN_MATH_ROW_INSERT:
654         case LFUN_MATH_ROW_DELETE:
655         case LFUN_MATH_COLUMN_INSERT:
656         case LFUN_MATH_COLUMN_DELETE:
657         {
658                 MathInset::idx_type idx = 0;
659                 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
660                 if (p) {
661                         mathcursor->popToEnclosingGrid();
662                         bv->lockedInsetStoreUndo(Undo::EDIT);
663                         char align = arg.size() ? arg[0] : 'c';
664                         switch (action) {
665                                 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
666                                 case LFUN_MATH_VALIGN: p->valign(align); break;
667                                 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
668                                 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
669                                 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
670                                 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
671                                 default: ;
672                         }
673                         updateLocal(bv, true);
674                 }
675                 break;
676         }
677
678         case LFUN_EXEC_COMMAND:
679                 result = UNDISPATCHED;
680                 break;
681
682         case LFUN_BREAKPARAGRAPH:
683         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
684                 //lyxerr << "LFUN ignored\n";
685                 break;
686
687         case LFUN_INSET_ERT:
688                 // interpret this as if a backslash was typed
689                 bv->lockedInsetStoreUndo(Undo::EDIT);
690                 mathcursor->interpret('\\');
691                 updateLocal(bv, true);
692                 break;
693
694         case -1:
695         case LFUN_INSERT_MATH:
696         case LFUN_SELFINSERT:
697                 if (!arg.empty()) {
698                         bv->lockedInsetStoreUndo(Undo::EDIT);
699                         if (arg.size() == 1)
700                                 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
701                         else
702                                 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
703                         updateLocal(bv, true);
704                 }
705                 break;
706
707         case LFUN_MATH_PANEL:
708                 result = UNDISPATCHED;
709                 break;
710
711         case LFUN_ESCAPE:
712                 if (mathcursor->selection())
713                         mathcursor->selClear();
714                 else
715                         result = UNDISPATCHED;
716                 break;
717
718         default:
719                 result = UNDISPATCHED;
720         }
721
722         mathcursor->normalize();
723         mathcursor->touch();
724
725         lyx::Assert(mathcursor);
726
727         if (mathcursor->selection() || was_selection)
728                 toggleInsetSelection(bv);
729
730         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
731             result == UNDISPATCHED)
732                 showInsetCursor(bv);
733         else
734                 bv->unlockInset(this);
735
736         revealCodes(bv);
737
738         return result;  // original version
739 }
740
741
742 void InsetFormulaBase::revealCodes(BufferView * /*bv*/) const
743 {
744         if (!mathcursor)
745                 return;
746 #if 0
747         // write something to the minibuffer
748         // translate to latex
749         mathcursor->markInsert();
750         ostringstream os;
751         write(NULL, os);
752         string str = os.str();
753         mathcursor->markErase();
754         string::size_type pos = 0;
755         string res;
756         for (string::iterator it = str.begin(); it != str.end(); ++it) {
757                 if (*it == '\n')
758                         res += ' ';
759                 else if (*it == '\0') {
760                         res += "  -X-  ";
761                         pos = it - str.begin();
762                 }
763                 else
764                         res += *it;
765         }
766         if (pos > 30)
767                 res = res.substr(pos - 30);
768         if (res.size() > 60)
769                 res = res.substr(0, 60);
770         bv->owner()->message(res);
771 #endif
772 }
773
774
775 Inset::Code InsetFormulaBase::lyxCode() const
776 {
777         return Inset::MATH_CODE;
778 }
779
780
781 int InsetFormulaBase::ylow() const
782 {
783         return yo_ - ascent(view_, font_);
784 }
785
786
787 int InsetFormulaBase::yhigh() const
788 {
789         return yo_ + descent(view_, font_);
790 }
791
792
793 int InsetFormulaBase::xlow() const
794 {
795         return xo_;
796 }
797
798
799 int InsetFormulaBase::xhigh() const
800 {
801         return xo_ + width(view_, font_);
802 }
803
804
805 /////////////////////////////////////////////////////////////////////
806
807
808 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
809                                      bool, bool)
810 {
811 #ifdef WITH_WARNINGS
812 #warning pretty ugly
813 #endif
814         static InsetFormulaBase * lastformula = 0;
815         static MathIterator current = MathIterator(ibegin(par().nucleus()));
816         static MathArray ar;
817         static string laststr;
818
819         if (lastformula != this || laststr != str) {
820                 //lyxerr << "reset lastformula to " << this << "\n";
821                 lastformula = this;
822                 laststr = str;
823                 current = ibegin(par().nucleus());
824                 ar.clear();
825                 mathed_parse_cell(ar, str);
826         } else {
827                 ++current;
828         }
829         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
830
831         for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
832                 if (it.cell().matchpart(ar, it.position().pos_)) {
833                         mathcursor->setSelection(it.cursor(), ar.size());
834                         current = it;
835                         it.jump(ar.size());
836                         // I guess some of the following can go
837                         bv->toggleSelection(true);
838                         hideInsetCursor(bv);
839                         updateLocal(bv, true);
840                         showInsetCursor(bv);
841                         metrics(bv);
842                         return true;
843                 }
844         }
845
846         //lyxerr << "not found!\n";
847         lastformula = 0;
848         // we have to unlock ourself in this function by default!
849         // don't ask me why...
850         bv->unlockInset(this);
851         return false;
852 }
853
854
855 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
856                                       bool a, bool b)
857 {
858         lyxerr << "searching backward not implemented in mathed" << endl;
859         return searchForward(bv, what, a, b);
860 }
861
862
863 /////////////////////////////////////////////////////////////////////
864
865
866 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
867 {
868         if (bv->available()) {
869                 // use selection if available..
870                 //string sel;
871                 //if (action == LFUN_MATH_IMPORT_SELECTION)
872                 //      sel = "";
873                 //else
874
875                 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
876
877                 InsetFormulaBase * f;
878                 if (sel.empty()) {
879                         f = new InsetFormula;
880                         if (openNewInset(bv, f)) {
881                                 // don't do that also for LFUN_MATH_MODE unless you want end up with
882                                 // always changing to mathrm when opening an inlined inset
883                                 // -- I really hate "LyXfunc overloading"...
884                                 if (display)
885                                         f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
886                                 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
887                         }
888                 } else {
889                         // create a macro if we see "\\newcommand" somewhere, and an ordinary
890                         // formula otherwise
891                         if (sel.find("\\newcommand") == string::npos &&
892                                   sel.find("\\def") == string::npos)
893                                 f = new InsetFormula(sel);
894                         else {
895                                 string name;
896                                 if (!mathed_parse_macro(name, sel))
897                                         return;
898                                 f = new InsetFormulaMacro(sel);
899                         }
900                         bv->getLyXText()->cutSelection(bv);
901                         openNewInset(bv, f);
902                 }
903         }
904         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
905 }
906
907
908 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
909 {
910         mathDispatchCreation(bv, arg, true);
911 }
912
913
914 void mathDispatchMathMode(BufferView * bv, string const & arg)
915 {
916         mathDispatchCreation(bv, arg, false);
917 }
918
919
920 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
921 {
922         mathDispatchCreation(bv, arg, true);
923 }
924
925
926 void mathDispatchMathMacro(BufferView * bv, string const & arg)
927 {
928         if (bv->available()) {
929                 if (arg.empty())
930                         bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
931                 else {
932                         string s = arg;
933                         string const s1 = token(s, ' ', 1);
934                         int const na = s1.empty() ? 0 : lyx::atoi(s1);
935                         openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
936                 }
937         }
938 }
939
940
941 void mathDispatchMathDelim(BufferView * bv, string const & arg)
942 {
943         if (bv->available()) {
944                 if (openNewInset(bv, new InsetFormula))
945                         bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
946         }
947 }
948
949
950 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
951 {
952         if (bv->available()) {
953                 if (openNewInset(bv, new InsetFormula))
954                         bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
955         }
956 }
957
958
959 void mathDispatchInsertMath(BufferView * bv, string const & arg)
960 {
961         if (bv->available()) {
962                 if (arg.size() && arg[0] == '\\') {
963                         InsetFormula * f = new InsetFormula(arg);
964                         if (!bv->insertInset(f))
965                                 delete f;
966                         else if (!mathcursor) // hotfix 
967                                 bv->getLyXText()->cursorRight(bv);
968                 } else {
969                         mathDispatchMathMode(bv, arg);
970                 }
971         }
972 }
973
974
975 void mathDispatchGreek(BufferView * bv, string const & arg)
976 {
977         if (bv->available()) {
978                 InsetFormula * f = new InsetFormula;
979                 if (openNewInset(bv, f)) {
980                         bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
981                         bv->unlockInset(f);
982                 }
983         }
984 }
985
986
987 void mathDispatch(BufferView *, kb_action, string const &)
988 {}