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