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