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