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