]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
make \newcommand{\bb}[1]{\mathbf{#1}} work for read/write/display.
[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         case LFUN_FREE:         handleFont(bv, LM_TC_TEXTRM); break;
523
524         case LFUN_GREEK: 
525                 handleFont(bv, LM_TC_GREEK1);
526                 if (arg.size())
527                         mathcursor->interpret(arg);
528                 break;
529
530         case LFUN_MATH_MODE:
531                 //handleFont(bv, LM_TC_TEXTRM);
532
533                 //mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
534                 //updateLocal(bv, true);
535
536                 //bv->owner()->message(_("math text mode toggled"));
537                 break;
538
539         case LFUN_MATH_LIMITS:
540                 bv->lockedInsetStoreUndo(Undo::EDIT);
541                 if (mathcursor->toggleLimits())
542                         updateLocal(bv, true);
543                 break;
544
545         case LFUN_MATH_SIZE:
546 #if 0
547                 if (!arg.empty()) {
548                         bv->lockedInsetStoreUndo(Undo::EDIT);
549                         mathcursor->setSize(arg);
550                         updateLocal(bv, true);
551                 }
552 #endif
553                 break;
554
555         case LFUN_INSERT_MATRIX:
556                 if (!arg.empty()) {
557                         bv->lockedInsetStoreUndo(Undo::EDIT);
558                         mathcursor->interpret("matrix " + arg);
559                         updateLocal(bv, true);
560                 }
561                 break;
562
563         case LFUN_MATH_SPACE:
564         {
565                 bv->lockedInsetStoreUndo(Undo::EDIT);
566                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
567                 updateLocal(bv, true);
568                 break;
569         }
570         
571         case LFUN_SUPERSCRIPT:
572         case LFUN_SUBSCRIPT:
573         {
574                 bv->lockedInsetStoreUndo(Undo::EDIT);
575                 mathcursor->script((action == LFUN_SUPERSCRIPT));
576                 updateLocal(bv, true);
577                 break;
578         }
579         
580         case LFUN_MATH_DELIM:
581         {
582                 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
583                 string ls;
584                 string rs;
585                 istringstream is(arg.c_str());
586                 is >> ls >> rs;
587                 if (!is) {
588                         lyxerr << "can't parse delimeters from '" << arg << "'\n";
589                         break;
590                 }
591                 bv->lockedInsetStoreUndo(Undo::EDIT);
592                 mathcursor->handleDelim(ls, rs);
593                 updateLocal(bv, true);
594                 break;
595         }
596
597         case LFUN_PROTECTEDSPACE:
598                 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
599                 bv->lockedInsetStoreUndo(Undo::EDIT);
600                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
601                 updateLocal(bv, true);
602                 break;
603
604         case LFUN_UNDO:
605                 bv->owner()->message(_("Invalid action in math mode!"));
606                 break;
607
608
609         case LFUN_MATH_HALIGN:
610         case LFUN_MATH_VALIGN:
611         case LFUN_MATH_ROW_INSERT:
612         case LFUN_MATH_ROW_DELETE:
613         case LFUN_MATH_COLUMN_INSERT:
614         case LFUN_MATH_COLUMN_DELETE:
615         {
616                 MathInset::idx_type idx = 0;
617                 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
618                 if (p) {
619                         bv->lockedInsetStoreUndo(Undo::EDIT);
620                         char al = arg.size() ? arg[0] : 'c';
621                         switch (action) {
622                                 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
623                                 case LFUN_MATH_VALIGN: p->valign(al); break;
624                                 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
625                                 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
626                                 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
627                                 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
628                                 default: ;
629                         }
630                         updateLocal(bv, true);
631                 }
632                 break;
633         }
634
635         case LFUN_EXEC_COMMAND:
636                 result = UNDISPATCHED;
637                 break;
638
639         case LFUN_BREAKPARAGRAPH:
640         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
641                 //lyxerr << "LFUN ignored\n";
642                 break;
643
644         case LFUN_INSET_ERT:
645                 // interpret this as if a backslash was typed
646                 bv->lockedInsetStoreUndo(Undo::EDIT);
647                 mathcursor->interpret("\\");
648                 updateLocal(bv, true);
649                 break;
650
651         case -1:
652         case LFUN_INSERT_MATH:
653         case LFUN_SELFINSERT:
654                 if (!arg.empty()) {
655                         bv->lockedInsetStoreUndo(Undo::EDIT);
656                         result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
657                         updateLocal(bv, true);
658                 }
659                 break;
660
661         case LFUN_MATH_PANEL:
662                 result = UNDISPATCHED;
663                 break;
664
665         case LFUN_ESCAPE:
666                 if (mathcursor->selection())
667                         mathcursor->selClear();
668                 else
669                         result = UNDISPATCHED;
670                 break;
671
672         default:
673                 result = UNDISPATCHED;
674         }
675
676         mathcursor->normalize();
677
678         lyx::Assert(mathcursor);
679
680         if (mathcursor->selection() || was_selection)
681                 toggleInsetSelection(bv);
682
683         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
684             result == UNDISPATCHED)
685                 showInsetCursor(bv);
686         else
687                 bv->unlockInset(this);
688
689         return result;  // original version
690 }
691
692
693 Inset::Code InsetFormulaBase::lyxCode() const
694 {
695         return Inset::MATH_CODE;
696 }
697
698
699 int InsetFormulaBase::ylow() const
700 {
701         return yo_ - ascent(view_, font_);
702 }
703
704
705 int InsetFormulaBase::yhigh() const
706 {
707         return yo_ + descent(view_, font_);
708 }
709
710
711 int InsetFormulaBase::xlow() const
712 {
713         return xo_;
714 }
715
716
717 int InsetFormulaBase::xhigh() const
718 {
719         return xo_ + width(view_, font_);
720 }
721
722
723 /////////////////////////////////////////////////////////////////////
724
725
726 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
727                    bool const &, bool const &)
728 {
729 #ifdef WITH_WARNINGS
730 #warning pretty ugly
731 #endif
732         static InsetFormulaBase * lastformula = 0;
733         static MathIterator current = MathIterator(ibegin(par().nucleus()));
734         static MathArray ar;
735         static string laststr;
736
737         if (lastformula != this || laststr != str) {
738                 //lyxerr << "reset lastformula to " << this << "\n";
739                 lastformula = this;
740                 laststr = str;
741                 current = ibegin(par().nucleus());
742                 ar.clear();
743                 mathed_parse_cell(ar, str);
744         } else {
745                 ++current;
746         }
747         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
748
749         for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
750                 if (it.cell().matchpart(ar, it.position().pos_)) {
751                         mathcursor->setSelection(it.cursor(), ar.size());
752                         current = it;
753                         it.jump(ar.size());
754                         // I guess some of the following can go
755                         bv->toggleSelection(true);
756                         hideInsetCursor(bv);
757                         updateLocal(bv, true);
758                         showInsetCursor(bv);
759                         metrics(bv);
760                         return true;
761                 }
762         }
763
764         //lyxerr << "not found!\n";
765         lastformula = 0;
766         // we have to unlock ourself in this function by default!
767         // don't ask me why...
768         bv->unlockInset(this);
769         return false;
770 }
771
772
773 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
774                     bool const & a, bool const & b)
775 {
776         lyxerr << "searching backward not implemented in mathed" << endl;
777         return searchForward(bv, what, a, b);
778 }
779
780
781 /////////////////////////////////////////////////////////////////////
782
783
784 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
785 {
786         if (bv->available()) {
787                 // use selection if available..
788                 //string sel;
789                 //if (action == LFUN_MATH_IMPORT_SELECTION)
790                 //      sel = "";
791                 //else
792
793                 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
794
795                 InsetFormulaBase * f;
796                 if (sel.empty()) {
797                         f = new InsetFormula;
798                         if (openNewInset(bv, f)) {
799                                 // don't do that also for LFUN_MATH_MODE unless you want end up with
800                                 // always changing to mathrm when opening an inlined inset
801                                 // -- I really hate "LyXfunc overloading"...
802                                 if (display)
803                                         f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
804                                 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
805                         }
806                 } else {
807                         // create a macro if we see "\\newcommand" somewhere, and an ordinary
808                         // formula otherwise
809                         if (sel.find("\\newcommand") == string::npos) 
810                                 f = new InsetFormula(sel);
811                         else
812                                 f = new InsetFormulaMacro(sel);
813                         bv->getLyXText()->cutSelection(bv);
814                         openNewInset(bv, f);
815                 }
816         }
817         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
818 }
819
820
821 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
822 {
823         mathDispatchCreation(bv, arg, true);
824 }
825
826         
827 void mathDispatchMathMode(BufferView * bv, string const & arg)
828 {
829         mathDispatchCreation(bv, arg, false);
830 }
831
832
833 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
834 {
835         mathDispatchCreation(bv, arg, true);
836 }
837
838
839 void mathDispatchMathMacro(BufferView * bv, string const & arg)
840 {
841         if (bv->available()) {
842                 if (arg.empty())
843                         bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
844                 else {
845                         string s(arg);
846                         string const s1 = token(s, ' ', 1);
847                         int const na = s1.empty() ? 0 : lyx::atoi(s1);
848                         openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
849                 }
850         }
851 }
852
853
854 void mathDispatchMathDelim(BufferView * bv, string const & arg)
855 {
856         if (bv->available()) { 
857                 if (openNewInset(bv, new InsetFormula))
858                         bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
859         }
860 }          
861
862
863 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
864 {          
865         if (bv->available()) { 
866                 if (openNewInset(bv, new InsetFormula))
867                         bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
868         }
869 }          
870
871
872 void mathDispatchInsertMath(BufferView * bv, string const & arg)
873 {
874         if (bv->available()) {
875                 if (arg.size() && arg[0] == '\\') {
876                         InsetFormula * f = new InsetFormula(arg);
877                         if (!bv->insertInset(f))
878                                 delete f;
879                 } else
880                         mathDispatchMathMode(bv, arg);
881         }
882 }
883
884
885 void mathDispatchGreek(BufferView * bv, string const & arg)
886 {          
887         if (bv->available()) { 
888                 InsetFormula * f = new InsetFormula;
889                 if (openNewInset(bv, f)) {
890                         bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
891                         bv->unlockInset(f);
892                 }
893         }
894 }          
895
896
897 void mathDispatch(BufferView *, kb_action, string const &)
898 {}