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