]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
support for color in math
[lyx.git] / src / mathed / formulabase.C
1 /**
2  * \file formulabase.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "formulabase.h"
15 #include "support/std_sstream.h"
16 #include "formula.h"
17 #include "formulamacro.h"
18 #include "funcrequest.h"
19 #include "BufferView.h"
20 #include "lyxtext.h"
21 #include "gettext.h"
22 #include "debug.h"
23 #include "math_support.h"
24 #include "support/lstrings.h"
25 #include "support/lyxlib.h"
26 #include "frontends/LyXView.h"
27 #include "math_arrayinset.h"
28 #include "math_deliminset.h"
29 #include "math_cursor.h"
30 #include "math_factory.h"
31 #include "math_hullinset.h"
32 #include "math_parser.h"
33 #include "math_spaceinset.h"
34 #include "undo_funcs.h"
35 #include "frontends/Dialogs.h"
36 #include "ref_inset.h"
37
38 using lyx::support::atoi;
39 using lyx::support::split;
40 using lyx::support::token;
41
42 using std::abs;
43 using std::endl;
44 using std::max;
45
46 using std::istringstream;
47
48
49 MathCursor * mathcursor = 0;
50
51 namespace {
52
53 // local global
54 int first_x;
55 int first_y;
56
57 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
58 {
59         if (!bv->insertInset(new_inset)) {
60                 delete new_inset;
61                 return false;
62         }
63         new_inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
64         return true;
65 }
66
67
68 } // namespace anon
69
70
71
72 InsetFormulaBase::InsetFormulaBase()
73         : view_(0), xo_(0), yo_(0)
74 {
75         // This is needed as long the math parser is not re-entrant
76         initMath();
77         //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
78         //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
79         //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
80         //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
81 }
82
83
84 // simply scrap this function if you want
85 void InsetFormulaBase::mutateToText()
86 {
87 #if 0
88         // translate to latex
89         ostringstream os;
90         latex(NULL, os, false, false);
91         string str = os.str();
92
93         // insert this text
94         LyXText * lt = view_->getLyXText();
95         string::const_iterator cit = str.begin();
96         string::const_iterator end = str.end();
97         for (; cit != end; ++cit)
98                 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
99
100         // remove ourselves
101         //view_->owner()->dispatch(LFUN_ESCAPE);
102 #endif
103 }
104
105
106 void InsetFormulaBase::handleFont
107         (BufferView * bv, string const & arg, string const & font)
108 {
109         // this whole function is a hack and won't work for incremental font
110         // changes...
111         recordUndo(bv, Undo::ATOMIC);
112
113         if (mathcursor->inset()->name() == font)
114                 mathcursor->handleFont(font);
115         else {
116                 mathcursor->handleNest(createMathInset(font));
117                 mathcursor->insert(arg);
118         }
119 }
120
121
122 BufferView * InsetFormulaBase::view() const
123 {
124         return view_;
125 }
126
127
128 void InsetFormulaBase::validate(LaTeXFeatures &) const
129 {}
130
131
132 string const InsetFormulaBase::editMessage() const
133 {
134         return _("Math editor mode");
135 }
136
137
138 void InsetFormulaBase::insetUnlock(BufferView * bv)
139 {
140         if (mathcursor) {
141                 if (mathcursor->inMacroMode()) {
142                         mathcursor->macroModeClose();
143                         bv->updateInset(this);
144                 }
145                 releaseMathCursor(bv);
146         }
147         if (bv->buffer())
148                 generatePreview(*bv->buffer());
149         bv->updateInset(this);
150 }
151
152
153 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
154 {
155         mathcursor->getPos(x, y);
156 }
157
158
159 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
160 {
161         // calling metrics here destroys the cached xo,yo positions e.g. in
162         // MathParboxinset. And it would be too expensive anyway...
163         //metrics(bv);
164         if (!mathcursor) {
165                 lyxerr << "getCursorPos - should not happen";
166                 x = y = 0;
167                 return;
168         }
169         mathcursor->getPos(x, y);
170         x = mathcursor->targetX();
171         x -= xo_;
172         y -= yo_;
173         //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
174 }
175
176
177 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
178 {
179         if (!mathcursor)
180                 return;
181         int x, y, asc, des;
182         asc = 10;
183         des = 2;
184         //math_font_max_dim(font_, asc, des);
185         getCursorPos(bv, x, y);
186         //y += yo_;
187         //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
188         bv->fitLockedInsetCursor(x, y, asc, des);
189 }
190
191
192 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
193 {
194         if (mathcursor)
195                 bv->updateInset(this);
196 }
197
198
199 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
200 {
201         if (!mathcursor)
202                 return UNDISPATCHED;
203
204         BufferView * bv = cmd.view();
205         bv->updateInset(this);
206         //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
207
208         if (cmd.button() == mouse_button::button3) {
209                 // try to dispatch to enclosed insets first
210                 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
211                         // launch math panel for right mouse button
212                         lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
213                         bv->owner()->getDialogs().show("mathpanel");
214                 }
215                 return DISPATCHED;
216         }
217
218         if (cmd.button() == mouse_button::button2) {
219                 MathArray ar;
220                 asArray(bv->getClipboard(), ar);
221                 mathcursor->selClear();
222                 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
223                 mathcursor->insert(ar);
224                 bv->updateInset(this);
225                 return DISPATCHED;
226         }
227
228         if (cmd.button() == mouse_button::button1) {
229                 // try to dispatch to enclosed insets first
230                 mathcursor->dispatch(cmd);
231                 cmd.view()->stuffClipboard(mathcursor->grabSelection());
232                 // try to set the cursor
233                 //delete mathcursor;
234                 //mathcursor = new MathCursor(this, x == 0);
235                 //metrics(bv);
236                 //mathcursor->setPos(x + xo_, y + yo_);
237                 return DISPATCHED;
238         }
239
240         return UNDISPATCHED;
241 }
242
243
244 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
245 {
246         BufferView * bv = cmd.view();
247         //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
248
249         if (!mathcursor || mathcursor->formula() != this) {
250                 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
251                 releaseMathCursor(bv);
252                 mathcursor = new MathCursor(this, cmd.x == 0);
253                 //metrics(bv);
254                 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
255         }
256
257         if (cmd.button() == mouse_button::button3) {
258                 mathcursor->dispatch(cmd);
259                 return DISPATCHED;
260         }
261
262         if (cmd.button() == mouse_button::button1) {
263                 first_x = cmd.x;
264                 first_y = cmd.y;
265                 mathcursor->selClear();
266                 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
267                 mathcursor->dispatch(cmd);
268                 return DISPATCHED;
269         }
270
271         bv->updateInset(this);
272         return DISPATCHED;
273 }
274
275
276 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
277 {
278         if (!mathcursor)
279                 return DISPATCHED;
280
281         if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
282                 return DISPATCHED;
283
284         // only select with button 1
285         if (cmd.button() != mouse_button::button1)
286                 return DISPATCHED;
287
288         if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
289                 return DISPATCHED;
290
291         first_x = cmd.x;
292         first_y = cmd.y;
293
294         if (!mathcursor->selection())
295                 mathcursor->selStart();
296
297         BufferView * bv = cmd.view();
298         mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
299         bv->updateInset(this);
300         return DISPATCHED;
301 }
302
303
304 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
305 {
306         lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
307                 << " arg: '" << cmd.argument
308                 << "' x: '" << cmd.x
309                 << " y: '" << cmd.y
310                 << "' button: " << cmd.button() << endl;
311
312         BufferView * bv = cmd.view();
313
314         // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
315         bool remove_inset = false;
316
317         switch (cmd.action) {
318                 case LFUN_INSET_EDIT:
319                         lyxerr << "Called EDIT with '" << cmd.argument << "'" << endl;
320                         if (!bv->lockInset(this))
321                                 lyxerr << "Cannot lock math inset in edit call!" << endl;
322                         releaseMathCursor(bv);
323                         if (!cmd.argument.empty()) {
324                                 mathcursor = new MathCursor(this, cmd.argument == "left");
325                                 //metrics(bv);
326                         } else {
327                                 mathcursor = new MathCursor(this, true);
328                                 //metrics(bv);
329                                 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
330                         }
331                         // if that is removed, we won't get the magenta box when entering an
332                         // inset for the first time
333                         bv->updateInset(this);
334                         return DISPATCHED;
335
336                 case LFUN_MOUSE_PRESS:
337                         //lyxerr << "Mouse single press" << endl;
338                         return lfunMousePress(cmd);
339                 case LFUN_MOUSE_MOTION:
340                         //lyxerr << "Mouse motion" << endl;
341                         return lfunMouseMotion(cmd);
342                 case LFUN_MOUSE_RELEASE:
343                         //lyxerr << "Mouse single release" << endl;
344                         return lfunMouseRelease(cmd);
345                 case LFUN_MOUSE_DOUBLE:
346                         //lyxerr << "Mouse double" << endl;
347                         return localDispatch(FuncRequest(LFUN_WORDSEL));
348                 default:
349                         break;
350         }
351
352         if (!mathcursor)
353                 return UNDISPATCHED;
354
355         string argument    = cmd.argument;
356         RESULT result      = DISPATCHED;
357         bool sel           = false;
358         bool was_macro     = mathcursor->inMacroMode();
359         bool was_selection = mathcursor->selection();
360
361         mathcursor->normalize();
362         mathcursor->touch();
363
364         switch (cmd.action) {
365
366         case LFUN_MATH_MUTATE:
367         case LFUN_MATH_DISPLAY:
368         case LFUN_MATH_NUMBER:
369         case LFUN_MATH_NONUMBER:
370         case LFUN_CELL_SPLIT:
371         case LFUN_BREAKLINE:
372         case LFUN_DELETE_LINE_FORWARD:
373         case LFUN_INSERT_LABEL:
374         case LFUN_MATH_EXTERN:
375         case LFUN_TABULAR_FEATURE:
376         case LFUN_PASTESELECTION:
377         case LFUN_MATH_LIMITS:
378                 recordUndo(bv, Undo::ATOMIC);
379                 mathcursor->dispatch(cmd);
380                 break;
381
382         case LFUN_RIGHTSEL:
383                 sel = true; // fall through...
384         case LFUN_RIGHT:
385                 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
386                 //lyxerr << "calling scroll 20" << endl;
387                 //scroll(bv, 20);
388                 // write something to the minibuffer
389                 //bv->owner()->message(mathcursor->info());
390                 break;
391
392         case LFUN_LEFTSEL:
393                 sel = true; // fall through
394         case LFUN_LEFT:
395                 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
396                 break;
397
398         case LFUN_UPSEL:
399                 sel = true; // fall through
400         case LFUN_UP:
401                 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
402                 break;
403
404         case LFUN_DOWNSEL:
405                 sel = true; // fall through
406         case LFUN_DOWN:
407                 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
408                 break;
409
410         case LFUN_WORDSEL:
411                 mathcursor->home(false);
412                 mathcursor->end(true);
413                 break;
414
415         case LFUN_UP_PARAGRAPHSEL:
416         case LFUN_UP_PARAGRAPH:
417         case LFUN_DOWN_PARAGRAPHSEL:
418         case LFUN_DOWN_PARAGRAPH:
419                 result = FINISHED;
420                 break;
421
422         case LFUN_HOMESEL:
423         case LFUN_WORDLEFTSEL:
424                 sel = true; // fall through
425         case LFUN_HOME:
426         case LFUN_WORDLEFT:
427                 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
428                 break;
429
430         case LFUN_ENDSEL:
431         case LFUN_WORDRIGHTSEL:
432                 sel = true; // fall through
433         case LFUN_END:
434         case LFUN_WORDRIGHT:
435                 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
436                 break;
437
438         case LFUN_PRIORSEL:
439         case LFUN_PRIOR:
440         case LFUN_BEGINNINGBUFSEL:
441         case LFUN_BEGINNINGBUF:
442                 result = FINISHED;
443                 break;
444
445         case LFUN_NEXTSEL:
446         case LFUN_NEXT:
447         case LFUN_ENDBUFSEL:
448         case LFUN_ENDBUF:
449                 result = FINISHED_RIGHT;
450                 break;
451
452         case LFUN_CELL_FORWARD:
453                 mathcursor->idxNext();
454                 break;
455
456         case LFUN_CELL_BACKWARD:
457                 mathcursor->idxPrev();
458                 break;
459
460         case LFUN_DELETE_WORD_BACKWARD:
461         case LFUN_BACKSPACE:
462                 recordUndo(bv, Undo::ATOMIC);
463                 if (!mathcursor->backspace()) {
464                         result = FINISHED;
465                         remove_inset = true;
466                 }
467                 break;
468
469         case LFUN_DELETE_WORD_FORWARD:
470         case LFUN_DELETE:
471                 recordUndo(bv, Undo::ATOMIC);
472                 if (!mathcursor->erase()) {
473                         result = FINISHED;
474                         remove_inset = true;
475                 }
476                 break;
477
478         //    case LFUN_GETXY:
479         //      sprintf(dispatch_buffer, "%d %d",);
480         //      dispatch_result = dispatch_buffer;
481         //      break;
482         case LFUN_SETXY: {
483                 lyxerr << "LFUN_SETXY broken!" << endl;
484                 int x = 0;
485                 int y = 0;
486                 istringstream is(cmd.argument.c_str());
487                 is >> x >> y;
488                 mathcursor->setPos(x, y);
489                 break;
490         }
491
492         case LFUN_PASTE: {
493                 size_t n = 0;
494                 istringstream is(cmd.argument.c_str());
495                 is >> n;
496                 if (was_macro)
497                         mathcursor->macroModeClose();
498                 recordUndo(bv, Undo::ATOMIC);
499                 mathcursor->selPaste(n);
500                 break;
501         }
502
503         case LFUN_CUT:
504                 recordUndo(bv, Undo::DELETE);
505                 mathcursor->selCut();
506                 break;
507
508         case LFUN_COPY:
509                 mathcursor->selCopy();
510                 break;
511
512
513         // Special casing for superscript in case of LyX handling
514         // dead-keys:
515         case LFUN_CIRCUMFLEX:
516                 if (cmd.argument.empty()) {
517                         // do superscript if LyX handles
518                         // deadkeys
519                         recordUndo(bv, Undo::ATOMIC);
520                         mathcursor->script(true);
521                 }
522                 break;
523
524         case LFUN_UMLAUT:
525         case LFUN_ACUTE:
526         case LFUN_GRAVE:
527         case LFUN_BREVE:
528         case LFUN_DOT:
529         case LFUN_MACRON:
530         case LFUN_CARON:
531         case LFUN_TILDE:
532         case LFUN_CEDILLA:
533         case LFUN_CIRCLE:
534         case LFUN_UNDERDOT:
535         case LFUN_TIE:
536         case LFUN_OGONEK:
537         case LFUN_HUNG_UMLAUT:
538                 break;
539
540         //  Math fonts
541         case LFUN_BOLD:         handleFont(bv, cmd.argument, "mathbf"); break;
542         case LFUN_SANS:         handleFont(bv, cmd.argument, "mathsf"); break;
543         case LFUN_EMPH:         handleFont(bv, cmd.argument, "mathcal"); break;
544         case LFUN_ROMAN:        handleFont(bv, cmd.argument, "mathrm"); break;
545         case LFUN_CODE:         handleFont(bv, cmd.argument, "texttt"); break;
546         case LFUN_FRAK:         handleFont(bv, cmd.argument, "mathfrak"); break;
547         case LFUN_ITAL:         handleFont(bv, cmd.argument, "mathit"); break;
548         case LFUN_NOUN:         handleFont(bv, cmd.argument, "mathbb"); break;
549         case LFUN_FREEFONT_APPLY:  handleFont(bv, cmd.argument, "textrm"); break;
550         case LFUN_DEFAULT:      handleFont(bv, cmd.argument, "textnormal"); break;
551
552         case LFUN_MATH_MODE:
553                 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
554                         mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
555                 else
556                         handleFont(bv, cmd.argument, "textrm");
557                 //bv->owner()->message(_("math text mode toggled"));
558                 break;
559
560         case LFUN_MATH_SIZE:
561 #if 0
562                 if (!arg.empty()) {
563                         recordUndo(bv, Undo::ATOMIC);
564                         mathcursor->setSize(arg);
565                 }
566 #endif
567                 break;
568
569         case LFUN_INSERT_MATRIX: {
570                 recordUndo(bv, Undo::ATOMIC);
571                 unsigned int m = 1;
572                 unsigned int n = 1;
573                 string v_align;
574                 string h_align;
575                 istringstream is(argument);
576                 is >> m >> n >> v_align >> h_align;
577                 m = max(1u, m);
578                 n = max(1u, n);
579                 v_align += 'c';
580                 mathcursor->niceInsert(
581                         MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
582                 break;
583         }
584
585         case LFUN_SUPERSCRIPT:
586         case LFUN_SUBSCRIPT:
587         {
588                 recordUndo(bv, Undo::ATOMIC);
589                 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
590                 break;
591         }
592
593         case LFUN_MATH_DELIM:
594         {
595                 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
596                 string ls;
597                 string rs = split(cmd.argument, ls, ' ');
598                 // Reasonable default values
599                 if (ls.empty())
600                         ls = '(';
601                 if (rs.empty())
602                         rs = ')';
603
604                 recordUndo(bv, Undo::ATOMIC);
605                 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
606                 break;
607         }
608
609         case LFUN_SPACE_INSERT:
610         case LFUN_MATH_SPACE:
611                 recordUndo(bv, Undo::ATOMIC);
612                 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
613                 break;
614
615         case LFUN_UNDO:
616                 bv->owner()->message(_("Invalid action in math mode!"));
617                 break;
618
619
620         case LFUN_EXEC_COMMAND:
621                 result = UNDISPATCHED;
622                 break;
623
624         case LFUN_INSET_ERT:
625                 // interpret this as if a backslash was typed
626                 recordUndo(bv, Undo::ATOMIC);
627                 mathcursor->interpret('\\');
628                 break;
629
630         case LFUN_BREAKPARAGRAPH:
631         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
632         case LFUN_BREAKPARAGRAPH_SKIP:
633                 argument = "\n";
634                 // fall through
635
636 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
637 // handling such that "self-insert" works on "arbitrary stuff" too, and
638 // math-insert only handles special math things like "matrix".
639         case LFUN_INSERT_MATH:
640                 recordUndo(bv, Undo::ATOMIC);
641                 mathcursor->niceInsert(argument);
642                 break;
643
644         case -1:
645         case LFUN_SELFINSERT:
646                 if (!argument.empty()) {
647                         recordUndo(bv, Undo::ATOMIC);
648                         if (argument.size() == 1)
649                                 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
650                         else
651                                 mathcursor->insert(argument);
652                 }
653                 break;
654
655         case LFUN_ESCAPE:
656                 if (mathcursor->selection())
657                         mathcursor->selClear();
658                 else
659                         result = UNDISPATCHED;
660                 break;
661
662         case LFUN_INSET_TOGGLE:
663                 mathcursor->insetToggle();
664                 break;
665
666         case LFUN_DIALOG_SHOW:
667                 result = UNDISPATCHED;
668                 break;
669
670         case LFUN_DIALOG_SHOW_NEW_INSET: {
671                 string const & name = argument;
672                 string data;
673                 if (name == "ref") {
674                         RefInset tmp(name);
675                         data = tmp.createDialogStr(name);
676                 }
677
678                 if (data.empty())
679                         result = UNDISPATCHED;
680                 else {
681                         bv->owner()->getDialogs().show(name, data, 0);
682                 }
683         }
684         break;
685
686         case LFUN_INSET_APPLY: {
687                 string const name = cmd.getArg(0);
688                 InsetBase * base =
689                         bv->owner()->getDialogs().getOpenInset(name);
690
691                 if (base) {
692                         FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
693                         result = base->localDispatch(fr);
694                 } else {
695                         MathArray ar;
696                         if (createMathInset_fromDialogStr(cmd.argument, ar)) {
697                                 mathcursor->insert(ar);
698                                 result = DISPATCHED;
699                         } else {
700                                 result = UNDISPATCHED;
701                         }
702                 }
703         }
704         break;
705
706         default:
707                 result = UNDISPATCHED;
708         }
709
710         if (result == DISPATCHED)
711                 bv->updateInset(this);
712
713         mathcursor->normalize();
714         mathcursor->touch();
715
716         BOOST_ASSERT(mathcursor);
717
718         if (mathcursor->selection() || was_selection)
719                 toggleInsetSelection(bv);
720
721         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
722             result == UNDISPATCHED) {
723                 fitInsetCursor(bv);
724                 revealCodes(bv);
725                 cmd.view()->stuffClipboard(mathcursor->grabSelection());
726         } else {
727                 releaseMathCursor(bv);
728                 bv->unlockInset(this);
729                 if (remove_inset)
730                         bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
731         }
732
733         return result;  // original version
734 }
735
736
737 void InsetFormulaBase::revealCodes(BufferView * bv) const
738 {
739         if (!mathcursor)
740                 return;
741         bv->owner()->message(mathcursor->info());
742
743 #if 0
744         // write something to the minibuffer
745         // translate to latex
746         mathcursor->markInsert();
747         ostringstream os;
748         write(NULL, os);
749         string str = os.str();
750         mathcursor->markErase();
751         string::size_type pos = 0;
752         string res;
753         for (string::iterator it = str.begin(); it != str.end(); ++it) {
754                 if (*it == '\n')
755                         res += ' ';
756                 else if (*it == '\0') {
757                         res += "  -X-  ";
758                         pos = it - str.begin();
759                 }
760                 else
761                         res += *it;
762         }
763         if (pos > 30)
764                 res = res.substr(pos - 30);
765         if (res.size() > 60)
766                 res = res.substr(0, 60);
767         bv->owner()->message(res);
768 #endif
769 }
770
771
772 InsetOld::Code InsetFormulaBase::lyxCode() const
773 {
774         return InsetOld::MATH_CODE;
775 }
776
777
778 int InsetFormulaBase::ylow() const
779 {
780         return yo_ - dim_.asc;
781 }
782
783
784 int InsetFormulaBase::yhigh() const
785 {
786         return yo_ + dim_.des;
787 }
788
789
790 int InsetFormulaBase::xlow() const
791 {
792         return xo_;
793 }
794
795
796 int InsetFormulaBase::xhigh() const
797 {
798         return xo_ + dim_.wid;
799 }
800
801
802 /////////////////////////////////////////////////////////////////////
803
804
805 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
806                                      bool, bool)
807 {
808 #ifdef WITH_WARNINGS
809 #warning pretty ugly
810 #endif
811         static InsetFormulaBase * lastformula = 0;
812         static MathIterator current = MathIterator(ibegin(par().nucleus()));
813         static MathArray ar;
814         static string laststr;
815
816         if (lastformula != this || laststr != str) {
817                 //lyxerr << "reset lastformula to " << this << endl;
818                 lastformula = this;
819                 laststr = str;
820                 current = ibegin(par().nucleus());
821                 ar.clear();
822                 mathed_parse_cell(ar, str);
823         } else {
824                 ++current;
825         }
826         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
827
828         for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
829                 if (it.cell().matchpart(ar, it.back().pos_)) {
830                         bv->unlockInset(bv->theLockingInset());
831                         if (!bv->lockInset(this)) {
832                                 lyxerr << "Cannot lock inset" << endl;
833                                 return false;
834                         }
835                         delete mathcursor;
836                         mathcursor = new MathCursor(this, true);
837                         //metrics(bv);
838                         mathcursor->setSelection(it, ar.size());
839                         current = it;
840                         it.jump(ar.size());
841                         bv->updateInset(this);
842                         return true;
843                 }
844         }
845
846         //lyxerr << "not found!" << endl;
847         lastformula = 0;
848         return false;
849 }
850
851
852 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
853                                       bool a, bool b)
854 {
855         lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
856         return searchForward(bv, what, a, b);
857 }
858
859
860 bool InsetFormulaBase::display() const
861 {
862         return par()->asHullInset() && par()->asHullInset()->display();
863 }
864
865
866 string InsetFormulaBase::selectionAsString() const
867 {
868         return mathcursor ? mathcursor->grabSelection() : string();
869 }
870
871 /////////////////////////////////////////////////////////////////////
872
873
874 void mathDispatchCreation(FuncRequest const & cmd, bool display)
875 {
876         BufferView * bv = cmd.view();
877         // use selection if available..
878         //string sel;
879         //if (action == LFUN_MATH_IMPORT_SELECTION)
880         //      sel = "";
881         //else
882
883         string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
884
885         if (sel.empty()) {
886                 InsetFormula * f = new InsetFormula(bv);
887                 if (openNewInset(bv, f)) {
888                         bv->theLockingInset()->
889                                 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
890                         // don't do that also for LFUN_MATH_MODE unless you want end up with
891                         // always changing to mathrm when opening an inlined inset
892                         // -- I really hate "LyXfunc overloading"...
893                         if (display)
894                                 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
895                         f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
896                 }
897         } else {
898                 // create a macro if we see "\\newcommand" somewhere, and an ordinary
899                 // formula otherwise
900                 InsetFormulaBase * f;
901                 if (sel.find("\\newcommand") == string::npos &&
902                                 sel.find("\\def") == string::npos)
903                         f = new InsetFormula(sel);
904                 else
905                         f = new InsetFormulaMacro(sel);
906                 bv->getLyXText()->cutSelection(true, false);
907                 openNewInset(bv, f);
908         }
909         cmd.message(N_("Math editor mode"));
910 }
911
912
913 void mathDispatch(FuncRequest const & cmd)
914 {
915         BufferView * bv = cmd.view();
916         if (!bv->available())
917                 return;
918
919         switch (cmd.action) {
920
921                 case LFUN_MATH_DISPLAY:
922                         mathDispatchCreation(cmd, true);
923                         break;
924
925                 case LFUN_MATH_MODE:
926                         mathDispatchCreation(cmd, false);
927                         break;
928
929                 case LFUN_MATH_IMPORT_SELECTION:
930                         mathDispatchCreation(cmd, false);
931                         break;
932
933                 case LFUN_MATH_MACRO:
934                         if (cmd.argument.empty())
935                                 cmd.errorMessage(N_("Missing argument"));
936                         else {
937                                 string s = cmd.argument;
938                                 string const s1 = token(s, ' ', 1);
939                                 int const nargs = s1.empty() ? 0 : atoi(s1);
940                                 string const s2 = token(s, ' ', 2);
941                                 string const type = s2.empty() ? "newcommand" : s2;
942                                 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
943                         }
944                         break;
945
946                 case LFUN_INSERT_MATH:
947                 case LFUN_INSERT_MATRIX:
948                 case LFUN_MATH_DELIM: {
949                         InsetFormula * f = new InsetFormula(bv);
950                         if (openNewInset(bv, f)) {
951                                 bv->theLockingInset()->
952                                         localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
953                                 bv->theLockingInset()->localDispatch(cmd);
954                         }
955                         break;
956                 }
957
958                 default:
959                         break;
960         }
961 }