]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
fix build, thesaurus
[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         switch (action) {
372
373                 // --- Cursor Movements ---------------------------------------------
374
375         case LFUN_RIGHTSEL:
376                 sel = true; // fall through...
377
378         case LFUN_RIGHT:
379                 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
380                 updateLocal(bv, false);
381                 // write something to the minibuffer
382                 //bv->owner()->message(mathcursor->info());
383                 break;
384
385
386         case LFUN_LEFTSEL:
387                 sel = true; // fall through
388
389         case LFUN_LEFT:
390                 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
391                 updateLocal(bv, false);
392                 break;
393
394
395         case LFUN_UPSEL:
396                 sel = true;
397
398         case LFUN_UP:
399                 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
400                 updateLocal(bv, false);
401                 break;
402
403
404         case LFUN_DOWNSEL:
405                 sel = true;
406
407         case LFUN_DOWN:
408                 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
409                 updateLocal(bv, false);
410                 break;
411
412         case LFUN_HOMESEL:
413                 sel = true;
414
415         case LFUN_HOME:
416                 mathcursor->home(sel);
417                 updateLocal(bv, false);
418                 break;
419
420         case LFUN_ENDSEL:
421                 sel = true;
422
423         case LFUN_END:
424                 mathcursor->end(sel);
425                 updateLocal(bv, false);
426                 break;
427
428         case LFUN_DELETE_LINE_FORWARD:
429                 bv->lockedInsetStoreUndo(Undo::DELETE);
430                 mathcursor->delLine();
431                 updateLocal(bv, true);
432                 break;
433
434         case LFUN_TAB:
435                 mathcursor->idxNext();
436                 updateLocal(bv, false);
437                 break;
438
439         case LFUN_SHIFT_TAB:
440                 mathcursor->idxPrev();
441                 updateLocal(bv, false);
442                 break;
443
444         case LFUN_TABINSERT:
445                 bv->lockedInsetStoreUndo(Undo::EDIT);
446                 mathcursor->splitCell();
447                 updateLocal(bv, true);
448                 break;
449
450         case LFUN_DELETE_WORD_BACKWARD:
451         case LFUN_BACKSPACE:
452                 bv->lockedInsetStoreUndo(Undo::DELETE);
453                 mathcursor->backspace();
454                 bv->updateInset(this, true);
455                 break;
456
457         case LFUN_DELETE_WORD_FORWARD:
458         case LFUN_DELETE:
459                 bv->lockedInsetStoreUndo(Undo::DELETE);
460                 mathcursor->erase();
461                 bv->updateInset(this, true);
462                 break;
463
464         //    case LFUN_GETXY:
465         //      sprintf(dispatch_buffer, "%d %d",);
466         //      dispatch_result = dispatch_buffer;
467         //      break;
468         case LFUN_SETXY: {
469                 lyxerr << "LFUN_SETXY broken!\n";
470                 int x = 0;
471                 int y = 0;
472                 istringstream is(arg.c_str());
473                 is >> x >> y;
474                 mathcursor->setPos(x, y);
475                 updateLocal(bv, false);
476                 break;
477         }
478
479         case LFUN_PASTE:
480                 if (was_macro)
481                         mathcursor->macroModeClose();
482                 bv->lockedInsetStoreUndo(Undo::EDIT);
483                 mathcursor->selPaste();
484                 updateLocal(bv, true);
485                 break;
486
487         case LFUN_CUT:
488                 bv->lockedInsetStoreUndo(Undo::DELETE);
489                 mathcursor->selCut();
490                 updateLocal(bv, true);
491                 break;
492
493         case LFUN_COPY:
494                 mathcursor->selCopy();
495                 break;
496
497         case LFUN_WORDRIGHTSEL:
498         case LFUN_WORDLEFTSEL:
499                 break;
500
501                 // --- accented characters ------------------------------
502
503         case LFUN_UMLAUT:       handleAccent(bv, "ddot"); break;
504         case LFUN_CIRCUMFLEX:   handleAccent(bv, "hat"); break;
505         case LFUN_GRAVE:        handleAccent(bv, "grave"); break;
506         case LFUN_ACUTE:        handleAccent(bv, "acute"); break;
507         case LFUN_TILDE:        handleAccent(bv, "tilde"); break;
508         case LFUN_MACRON:       handleAccent(bv, "bar"); break;
509         case LFUN_DOT:          handleAccent(bv, "dot"); break;
510         case LFUN_CARON:        handleAccent(bv, "check"); break;
511         case LFUN_BREVE:        handleAccent(bv, "breve"); break;
512         case LFUN_VECTOR:       handleAccent(bv, "vec"); break;
513         case LFUN_UNDERBAR:     handleAccent(bv, "underbar"); break;
514
515         //  Math fonts
516         case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
517         case LFUN_BOLD:         handleFont(bv, LM_TC_BF); break;
518         case LFUN_SANS:         handleFont(bv, LM_TC_SF); break;
519         case LFUN_EMPH:         handleFont(bv, LM_TC_CAL); break;
520         case LFUN_ROMAN:        handleFont(bv, LM_TC_RM); break;
521         case LFUN_CODE:         handleFont(bv, LM_TC_TT); break;
522         case LFUN_FRAK:         handleFont(bv, LM_TC_EUFRAK); break;
523         case LFUN_ITAL:         handleFont(bv, LM_TC_IT); break;
524         case LFUN_NOUN:         handleFont(bv, LM_TC_BB); break;
525         case LFUN_DEFAULT:      handleFont(bv, LM_TC_VAR); break;
526         case LFUN_FREE:         handleFont(bv, LM_TC_TEXTRM); break;
527
528         case LFUN_GREEK: 
529                 handleFont(bv, LM_TC_GREEK1);
530                 if (arg.size())
531                         mathcursor->interpret(arg);
532                 break;
533
534         case LFUN_MATH_MODE:
535                 //handleFont(bv, LM_TC_TEXTRM);
536
537                 //mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
538                 //updateLocal(bv, true);
539
540                 //bv->owner()->message(_("math text mode toggled"));
541                 break;
542
543         case LFUN_MATH_LIMITS:
544                 bv->lockedInsetStoreUndo(Undo::EDIT);
545                 if (mathcursor->toggleLimits())
546                         updateLocal(bv, true);
547                 break;
548
549         case LFUN_MATH_SIZE:
550 #if 0
551                 if (!arg.empty()) {
552                         bv->lockedInsetStoreUndo(Undo::EDIT);
553                         mathcursor->setSize(arg);
554                         updateLocal(bv, true);
555                 }
556 #endif
557                 break;
558
559         case LFUN_INSERT_MATRIX:
560                 if (!arg.empty()) {
561                         bv->lockedInsetStoreUndo(Undo::EDIT);
562                         mathcursor->interpret("matrix " + arg);
563                         updateLocal(bv, true);
564                 }
565                 break;
566
567         case LFUN_MATH_SPACE:
568         {
569                 bv->lockedInsetStoreUndo(Undo::EDIT);
570                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
571                 updateLocal(bv, true);
572                 break;
573         }
574         
575         case LFUN_SUPERSCRIPT:
576         case LFUN_SUBSCRIPT:
577         {
578                 bv->lockedInsetStoreUndo(Undo::EDIT);
579                 mathcursor->script((action == LFUN_SUPERSCRIPT));
580                 updateLocal(bv, true);
581                 break;
582         }
583         
584         case LFUN_MATH_DELIM:
585         {
586                 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
587                 string ls;
588                 string rs;
589                 istringstream is(arg.c_str());
590                 is >> ls >> rs;
591                 if (!is) {
592                         lyxerr << "can't parse delimeters from '" << arg << "'\n";
593                         break;
594                 }
595                 bv->lockedInsetStoreUndo(Undo::EDIT);
596                 mathcursor->handleDelim(ls, rs);
597                 updateLocal(bv, true);
598                 break;
599         }
600
601         case LFUN_PROTECTEDSPACE:
602                 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
603                 bv->lockedInsetStoreUndo(Undo::EDIT);
604                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
605                 updateLocal(bv, true);
606                 break;
607
608         case LFUN_UNDO:
609                 bv->owner()->message(_("Invalid action in math mode!"));
610                 break;
611
612
613         case LFUN_MATH_HALIGN:
614         case LFUN_MATH_VALIGN:
615         case LFUN_MATH_ROW_INSERT:
616         case LFUN_MATH_ROW_DELETE:
617         case LFUN_MATH_COLUMN_INSERT:
618         case LFUN_MATH_COLUMN_DELETE:
619         {
620                 MathInset::idx_type idx = 0;
621                 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
622                 if (p) {
623                         bv->lockedInsetStoreUndo(Undo::EDIT);
624                         char al = arg.size() ? arg[0] : 'c';
625                         switch (action) {
626                                 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
627                                 case LFUN_MATH_VALIGN: p->valign(al); break;
628                                 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
629                                 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
630                                 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
631                                 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
632                                 default: ;
633                         }
634                         updateLocal(bv, true);
635                 }
636                 break;
637         }
638
639         case LFUN_EXEC_COMMAND:
640                 result = UNDISPATCHED;
641                 break;
642
643         case LFUN_BREAKPARAGRAPH:
644         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
645                 //lyxerr << "LFUN ignored\n";
646                 break;
647
648         case LFUN_INSET_ERT:
649                 // interpret this as if a backslash was typed
650                 bv->lockedInsetStoreUndo(Undo::EDIT);
651                 mathcursor->interpret("\\");
652                 updateLocal(bv, true);
653                 break;
654
655         case -1:
656         case LFUN_INSERT_MATH:
657         case LFUN_SELFINSERT:
658                 if (!arg.empty()) {
659                         bv->lockedInsetStoreUndo(Undo::EDIT);
660                         result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
661                         updateLocal(bv, true);
662                 }
663                 break;
664
665         case LFUN_MATH_PANEL:
666                 result = UNDISPATCHED;
667                 break;
668
669         case LFUN_ESCAPE:
670                 if (mathcursor->selection())
671                         mathcursor->selClear();
672                 else
673                         result = UNDISPATCHED;
674                 break;
675
676         default:
677                 result = UNDISPATCHED;
678         }
679
680         mathcursor->normalize();
681
682         lyx::Assert(mathcursor);
683
684         if (mathcursor->selection() || was_selection)
685                 toggleInsetSelection(bv);
686
687         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
688             result == UNDISPATCHED)
689                 showInsetCursor(bv);
690         else
691                 bv->unlockInset(this);
692
693         return result;  // original version
694 }
695
696
697 Inset::Code InsetFormulaBase::lyxCode() const
698 {
699         return Inset::MATH_CODE;
700 }
701
702
703 int InsetFormulaBase::ylow() const
704 {
705         return yo_ - ascent(view_, font_);
706 }
707
708
709 int InsetFormulaBase::yhigh() const
710 {
711         return yo_ + descent(view_, font_);
712 }
713
714
715 int InsetFormulaBase::xlow() const
716 {
717         return xo_;
718 }
719
720
721 int InsetFormulaBase::xhigh() const
722 {
723         return xo_ + width(view_, font_);
724 }
725
726
727 /////////////////////////////////////////////////////////////////////
728
729
730 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
731                                      bool, bool)
732 {
733 #ifdef WITH_WARNINGS
734 #warning pretty ugly
735 #endif
736         static InsetFormulaBase * lastformula = 0;
737         static MathIterator current = MathIterator(ibegin(par().nucleus()));
738         static MathArray ar;
739         static string laststr;
740
741         if (lastformula != this || laststr != str) {
742                 //lyxerr << "reset lastformula to " << this << "\n";
743                 lastformula = this;
744                 laststr = str;
745                 current = ibegin(par().nucleus());
746                 ar.clear();
747                 mathed_parse_cell(ar, str);
748         } else {
749                 ++current;
750         }
751         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
752
753         for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
754                 if (it.cell().matchpart(ar, it.position().pos_)) {
755                         mathcursor->setSelection(it.cursor(), ar.size());
756                         current = it;
757                         it.jump(ar.size());
758                         // I guess some of the following can go
759                         bv->toggleSelection(true);
760                         hideInsetCursor(bv);
761                         updateLocal(bv, true);
762                         showInsetCursor(bv);
763                         metrics(bv);
764                         return true;
765                 }
766         }
767
768         //lyxerr << "not found!\n";
769         lastformula = 0;
770         // we have to unlock ourself in this function by default!
771         // don't ask me why...
772         bv->unlockInset(this);
773         return false;
774 }
775
776
777 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
778                                       bool a, bool b)
779 {
780         lyxerr << "searching backward not implemented in mathed" << endl;
781         return searchForward(bv, what, a, b);
782 }
783
784
785 /////////////////////////////////////////////////////////////////////
786
787
788 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
789 {
790         if (bv->available()) {
791                 // use selection if available..
792                 //string sel;
793                 //if (action == LFUN_MATH_IMPORT_SELECTION)
794                 //      sel = "";
795                 //else
796
797                 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
798
799                 InsetFormulaBase * f;
800                 if (sel.empty()) {
801                         f = new InsetFormula;
802                         if (openNewInset(bv, f)) {
803                                 // don't do that also for LFUN_MATH_MODE unless you want end up with
804                                 // always changing to mathrm when opening an inlined inset
805                                 // -- I really hate "LyXfunc overloading"...
806                                 if (display)
807                                         f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
808                                 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
809                         }
810                 } else {
811                         // create a macro if we see "\\newcommand" somewhere, and an ordinary
812                         // formula otherwise
813                         if (sel.find("\\newcommand") == string::npos) 
814                                 f = new InsetFormula(sel);
815                         else
816                                 f = new InsetFormulaMacro(sel);
817                         bv->getLyXText()->cutSelection(bv);
818                         openNewInset(bv, f);
819                 }
820         }
821         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
822 }
823
824
825 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
826 {
827         mathDispatchCreation(bv, arg, true);
828 }
829
830         
831 void mathDispatchMathMode(BufferView * bv, string const & arg)
832 {
833         mathDispatchCreation(bv, arg, false);
834 }
835
836
837 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
838 {
839         mathDispatchCreation(bv, arg, true);
840 }
841
842
843 void mathDispatchMathMacro(BufferView * bv, string const & arg)
844 {
845         if (bv->available()) {
846                 if (arg.empty())
847                         bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
848                 else {
849                         string s(arg);
850                         string const s1 = token(s, ' ', 1);
851                         int const na = s1.empty() ? 0 : lyx::atoi(s1);
852                         openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
853                 }
854         }
855 }
856
857
858 void mathDispatchMathDelim(BufferView * bv, string const & arg)
859 {
860         if (bv->available()) { 
861                 if (openNewInset(bv, new InsetFormula))
862                         bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
863         }
864 }          
865
866
867 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
868 {          
869         if (bv->available()) { 
870                 if (openNewInset(bv, new InsetFormula))
871                         bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
872         }
873 }          
874
875
876 void mathDispatchInsertMath(BufferView * bv, string const & arg)
877 {
878         if (bv->available()) {
879                 if (arg.size() && arg[0] == '\\') {
880                         InsetFormula * f = new InsetFormula(arg);
881                         if (!bv->insertInset(f))
882                                 delete f;
883                 } else
884                         mathDispatchMathMode(bv, arg);
885         }
886 }
887
888
889 void mathDispatchGreek(BufferView * bv, string const & arg)
890 {          
891         if (bv->available()) { 
892                 InsetFormula * f = new InsetFormula;
893                 if (openNewInset(bv, f)) {
894                         bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
895                         bv->unlockInset(f);
896                 }
897         }
898 }          
899
900
901 void mathDispatch(BufferView *, kb_action, string const &)
902 {}