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