]> git.lyx.org Git - lyx.git/blob - src/mathed/MathMacro.cpp
Fix bug 5802 (http://bugzilla.lyx.org/show_bug.cgi?id=5802)
[lyx.git] / src / mathed / MathMacro.cpp
1 /**
2  * \file MathMacro.cpp
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  * \author Stefan Schimanski
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "MathMacro.h"
16
17 #include "InsetMathChar.h"
18 #include "MathCompletionList.h"
19 #include "MathExtern.h"
20 #include "MathStream.h"
21 #include "MathSupport.h"
22
23 #include "Buffer.h"
24 #include "BufferView.h"
25 #include "CoordCache.h"
26 #include "Cursor.h"
27 #include "FuncStatus.h"
28 #include "FuncRequest.h"
29 #include "LaTeXFeatures.h"
30 #include "LyXFunc.h"
31 #include "LyXRC.h"
32 #include "Undo.h"
33
34 #include "frontends/Painter.h"
35
36 #include "support/debug.h"
37 #include "support/lassert.h"
38 #include "support/textutils.h"
39
40 #include <ostream>
41 #include <vector>
42
43 using namespace std;
44
45 namespace lyx {
46
47
48 /// A proxy for the macro values
49 class ArgumentProxy : public InsetMath {
50 public:
51         ///
52         ArgumentProxy(MathMacro & mathMacro, size_t idx) 
53                 : mathMacro_(mathMacro), idx_(idx) {}
54         ///
55         ArgumentProxy(MathMacro & mathMacro, size_t idx, docstring const & def) 
56                 : mathMacro_(mathMacro), idx_(idx) 
57         {
58                         asArray(def, def_);
59         }
60         ///
61         void metrics(MetricsInfo & mi, Dimension & dim) const {
62                 mathMacro_.macro()->unlock();
63                 mathMacro_.cell(idx_).metrics(mi, dim);
64
65                 if (!mathMacro_.editMetrics(mi.base.bv)
66                     && mathMacro_.cell(idx_).empty())
67                         def_.metrics(mi, dim);
68
69                 mathMacro_.macro()->lock();
70         }
71         ///
72         void draw(PainterInfo & pi, int x, int y) const {
73                 if (mathMacro_.editMetrics(pi.base.bv)) {
74                         // The only way a ArgumentProxy can appear is in a cell of the 
75                         // MathMacro. Moreover the cells are only drawn in the DISPLAY_FOLDED 
76                         // mode and then, if the macro is edited the monochrome 
77                         // mode is entered by the MathMacro before calling the cells' draw
78                         // method. Then eventually this code is reached and the proxy leaves
79                         // monochrome mode temporarely. Hence, if it is not in monochrome 
80                         // here (and the assert triggers in pain.leaveMonochromeMode()) 
81                         // it's a bug.
82                         pi.pain.leaveMonochromeMode();
83                         mathMacro_.cell(idx_).draw(pi, x, y);
84                         pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
85                 } else if (mathMacro_.cell(idx_).empty()) {
86                         mathMacro_.cell(idx_).setXY(*pi.base.bv, x, y);
87                         def_.draw(pi, x, y);
88                 } else
89                         mathMacro_.cell(idx_).draw(pi, x, y);
90         }
91         ///
92         size_t idx() const { return idx_; }
93         ///
94         int kerning(BufferView const * bv) const
95         { 
96                 if (mathMacro_.editMetrics(bv)
97                     || !mathMacro_.cell(idx_).empty())
98                         return mathMacro_.cell(idx_).kerning(bv); 
99                 else
100                         return def_.kerning(bv);
101         }
102
103 private:
104         ///
105         Inset * clone() const 
106         {
107                 return new ArgumentProxy(*this);
108         }
109         ///
110         MathMacro & mathMacro_;
111         ///
112         size_t idx_;
113         ///
114         MathData def_;
115 };
116
117
118 MathMacro::MathMacro(docstring const & name)
119         : InsetMathNest(0), name_(name), displayMode_(DISPLAY_INIT),
120                 attachedArgsNum_(0), optionals_(0), nextFoldMode_(true),
121                 macro_(0), needsUpdate_(false), appetite_(9)
122 {}
123
124
125 Inset * MathMacro::clone() const
126 {
127         MathMacro * copy = new MathMacro(*this);
128         copy->needsUpdate_ = true;
129         copy->expanded_.cell(0).clear();
130         return copy;
131 }
132
133
134 docstring MathMacro::name() const
135 {
136         if (displayMode_ == DISPLAY_UNFOLDED)
137                 return asString(cell(0));
138
139         return name_;
140 }
141
142
143 void MathMacro::cursorPos(BufferView const & bv,
144                 CursorSlice const & sl, bool boundary, int & x, int & y) const
145 {
146         // We may have 0 arguments, but InsetMathNest requires at least one.
147         if (nargs() > 0)
148                 InsetMathNest::cursorPos(bv, sl, boundary, x, y);
149 }
150
151
152 bool MathMacro::editMode(BufferView const * bv) const {
153         // find this in cursor trace
154         Cursor const & cur = bv->cursor();
155         for (size_t i = 0; i != cur.depth(); ++i)
156                 if (&cur[i].inset() == this) {
157                         // look if there is no other macro in edit mode above
158                         ++i;
159                         for (; i != cur.depth(); ++i) {
160                                 MathMacro const * macro = dynamic_cast<MathMacro const *>(&cur[i].inset());
161                                 if (macro && macro->displayMode() == DISPLAY_NORMAL)
162                                         return false;
163                         }
164
165                         // ok, none found, I am the highest one
166                         return true;
167                 }
168
169         return false;
170 }
171
172
173 bool MathMacro::editMetrics(BufferView const * bv) const
174 {
175         return editing_[bv];
176 }
177
178
179 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
180 {
181         // set edit mode for which we will have calculated metrics. But only
182         editing_[mi.base.bv] = editMode(mi.base.bv);
183
184         // calculate new metrics according to display mode
185         if (displayMode_ == DISPLAY_INIT || displayMode_ == DISPLAY_INTERACTIVE_INIT) {
186                 mathed_string_dim(mi.base.font, from_ascii("\\") + name(), dim);
187         } else if (displayMode_ == DISPLAY_UNFOLDED) {
188                 cell(0).metrics(mi, dim);
189                 Dimension bsdim;
190                 mathed_string_dim(mi.base.font, from_ascii("\\"), bsdim);
191                 dim.wid += bsdim.width() + 1;
192                 dim.asc = max(bsdim.ascent(), dim.ascent());
193                 dim.des = max(bsdim.descent(), dim.descent());
194                 metricsMarkers(dim);
195         } else if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_LIST 
196                    && editing_[mi.base.bv]) {
197                 // Macro will be edited in a old-style list mode here:
198
199                 LASSERT(macro_ != 0, /**/);
200                 Dimension fontDim;
201                 FontInfo labelFont = sane_font;
202                 math_font_max_dim(labelFont, fontDim.asc, fontDim.des);
203                 
204                 // get dimension of components of list view
205                 Dimension nameDim;
206                 nameDim.wid = mathed_string_width(mi.base.font, from_ascii("Macro \\") + name() + ": ");
207                 nameDim.asc = fontDim.asc;
208                 nameDim.des = fontDim.des;
209
210                 Dimension argDim;
211                 argDim.wid = mathed_string_width(labelFont, from_ascii("#9: "));
212                 argDim.asc = fontDim.asc;
213                 argDim.des = fontDim.des;
214                 
215                 Dimension defDim;
216                 definition_.metrics(mi, defDim);
217                 
218                 // add them up
219                 dim.wid = nameDim.wid + defDim.wid;
220                 dim.asc = max(nameDim.asc, defDim.asc);
221                 dim.des = max(nameDim.des, defDim.des);
222                 
223                 for (idx_type i = 0; i < nargs(); ++i) {
224                         Dimension cdim;
225                         cell(i).metrics(mi, cdim);
226                         dim.des += max(argDim.height(), cdim.height()) + 1;
227                         dim.wid = max(dim.wid, argDim.wid + cdim.wid);
228                 }
229                 
230                 // make space for box and markers, 2 pixels
231                 dim.asc += 1;
232                 dim.des += 1;
233                 dim.wid += 2;
234                 metricsMarkers2(dim);
235         } else {
236                 LASSERT(macro_ != 0, /**/);
237
238                 // calculate metrics, hoping that all cells are seen
239                 macro_->lock();
240                 expanded_.cell(0).metrics(mi, dim);
241
242                 // otherwise do a manual metrics call
243                 CoordCache & coords = mi.base.bv->coordCache();
244                 for (idx_type i = 0; i < nargs(); ++i) {
245                         if (!coords.getArrays().has(&cell(i))) {
246                                 Dimension tdim;
247                                 cell(i).metrics(mi, tdim);
248                         }
249                 }
250                 macro_->unlock();
251
252                 // calculate dimension with label while editing
253                 if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_INLINE_BOX 
254                     && editing_[mi.base.bv]) {
255                         FontInfo font = mi.base.font;
256                         augmentFont(font, from_ascii("lyxtex"));
257                         Dimension namedim;
258                         mathed_string_dim(font, name(), namedim);
259 #if 0
260                         dim.wid += 2 + namedim.wid + 2 + 2;
261                         dim.asc = max(dim.asc, namedim.asc) + 2;
262                         dim.des = max(dim.des, namedim.des) + 2;
263 #endif
264                         dim.wid = max(1 + namedim.wid + 1, 2 + dim.wid + 2);
265                         dim.asc += 1 + namedim.height() + 1;
266                         dim.des += 2;
267                 }
268          
269         }
270 }
271
272
273 int MathMacro::kerning(BufferView const * bv) const {
274         if (displayMode_ == DISPLAY_NORMAL && !editing_[bv])
275                 return expanded_.kerning(bv);
276         else
277                 return 0;
278 }
279
280
281 void MathMacro::updateMacro(MacroContext const & mc) 
282 {
283         if (validName()) {
284                 macro_ = mc.get(name());            
285                 if (macro_ && macroBackup_ != *macro_) {
286                         macroBackup_ = *macro_;
287                         needsUpdate_ = true;
288                 }
289         } else {
290                 macro_ = 0;
291         }
292 }
293
294
295 void MathMacro::updateRepresentation()
296 {
297         // known macro?
298         if (macro_ == 0)
299                 return;
300
301         // update requires
302         requires_ = macro_->requires();
303         
304         // non-normal mode? We are done!
305         if (displayMode_ != DISPLAY_NORMAL)
306                 return;
307
308         // macro changed?
309         if (!needsUpdate_)
310                 return;
311         
312         needsUpdate_ = false;
313         
314         // get default values of macro
315         vector<docstring> const & defaults = macro_->defaults();
316         
317         // create MathMacroArgumentValue objects pointing to the cells of the macro
318         vector<MathData> values(nargs());
319         for (size_t i = 0; i < nargs(); ++i) {
320                 ArgumentProxy * proxy;
321                 if (i < defaults.size()) 
322                         proxy = new ArgumentProxy(*this, i, defaults[i]);
323                 else
324                         proxy = new ArgumentProxy(*this, i);
325                 values[i].insert(0, MathAtom(proxy));
326         }
327         
328         // expanding macro with the values
329         macro_->expand(values, expanded_.cell(0));
330                 // get definition for list edit mode
331         docstring const & display = macro_->display();
332         asArray(display.empty() ? macro_->definition() : display, definition_);
333 }
334
335
336 void MathMacro::draw(PainterInfo & pi, int x, int y) const
337 {
338         Dimension const dim = dimension(*pi.base.bv);
339
340         setPosCache(pi, x, y);
341         int expx = x;
342         int expy = y;
343
344         if (displayMode_ == DISPLAY_INIT || displayMode_ == DISPLAY_INTERACTIVE_INIT) {         
345                 FontSetChanger dummy(pi.base, "lyxtex");
346                 pi.pain.text(x, y, from_ascii("\\") + name(), pi.base.font);
347         } else if (displayMode_ == DISPLAY_UNFOLDED) {
348                 FontSetChanger dummy(pi.base, "lyxtex");
349                 pi.pain.text(x, y, from_ascii("\\"), pi.base.font);
350                 x += mathed_string_width(pi.base.font, from_ascii("\\")) + 1;
351                 cell(0).draw(pi, x, y);
352                 drawMarkers(pi, expx, expy);
353         } else if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_LIST
354                    && editing_[pi.base.bv]) {
355                 // Macro will be edited in a old-style list mode here:
356                 
357                 CoordCache const & coords = pi.base.bv->coordCache();
358                 FontInfo const & labelFont = sane_font;
359                 
360                 // markers and box needs two pixels
361                 x += 2;
362                 
363                 // get maximal font height
364                 Dimension fontDim;
365                 math_font_max_dim(pi.base.font, fontDim.asc, fontDim.des);
366                 
367                 // draw label
368                 docstring label = from_ascii("Macro \\") + name() + from_ascii(": ");
369                 pi.pain.text(x, y, label, labelFont);
370                 x += mathed_string_width(labelFont, label);
371
372                 // draw definition
373                 definition_.draw(pi, x, y);
374                 Dimension const & defDim = coords.getArrays().dim(&definition_);
375                 y += max(fontDim.des, defDim.des);
376                                 
377                 // draw parameters
378                 docstring str = from_ascii("#9");
379                 int strw1 = mathed_string_width(labelFont, from_ascii("#9"));
380                 int strw2 = mathed_string_width(labelFont, from_ascii(": "));
381                 
382                 for (idx_type i = 0; i < nargs(); ++i) {
383                         // position of label
384                         Dimension const & cdim = coords.getArrays().dim(&cell(i));
385                         x = expx + 2;
386                         y += max(fontDim.asc, cdim.asc) + 1;
387                         
388                         // draw label
389                         str[1] = '1' + i;
390                         pi.pain.text(x, y, str, labelFont);
391                         x += strw1;
392                         pi.pain.text(x, y, from_ascii(":"), labelFont);
393                         x += strw2;
394                         
395                         // draw paramter
396                         cell(i).draw(pi, x, y);
397                         
398                         // next line
399                         y += max(fontDim.des, cdim.des);
400                 }
401                 
402                 pi.pain.rectangle(expx + 1, expy - dim.asc + 1, dim.wid - 3, 
403                                   dim.height() - 2, Color_mathmacroframe);
404                 drawMarkers2(pi, expx, expy);
405         } else {
406                 bool drawBox = lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_INLINE_BOX;
407                 
408                 // warm up cells
409                 for (size_t i = 0; i < nargs(); ++i)
410                         cell(i).setXY(*pi.base.bv, x, y);
411
412                 if (drawBox && editing_[pi.base.bv]) {
413                         // draw header and rectangle around
414                         FontInfo font = pi.base.font;
415                         augmentFont(font, from_ascii("lyxtex"));
416                         font.setSize(FONT_SIZE_TINY);
417                         font.setColor(Color_mathmacrolabel);
418                         Dimension namedim;
419                         mathed_string_dim(font, name(), namedim);
420
421                         pi.pain.fillRectangle(x, y - dim.asc, dim.wid, 1 + namedim.height() + 1, Color_mathmacrobg);
422                         pi.pain.text(x + 1, y - dim.asc + namedim.asc + 2, name(), font);
423                         expx += (dim.wid - expanded_.cell(0).dimension(*pi.base.bv).width()) / 2;
424                 }
425
426                 if (editing_[pi.base.bv]) {
427                         pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
428                         expanded_.cell(0).draw(pi, expx, expy);
429                         pi.pain.leaveMonochromeMode();
430
431                         if (drawBox)
432                                 pi.pain.rectangle(x, y - dim.asc, dim.wid, 
433                                                   dim.height(), Color_mathmacroframe);
434                 } else
435                         expanded_.cell(0).draw(pi, expx, expy);
436
437                 if (!drawBox)
438                         drawMarkers(pi, x, y);
439         }
440
441         // edit mode changed?
442         if (editing_[pi.base.bv] != editMode(pi.base.bv))
443                 pi.base.bv->cursor().updateFlags(Update::SinglePar);
444 }
445
446
447 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
448 {
449         // We may have 0 arguments, but InsetMathNest requires at least one.
450         if (cells_.size() > 0)
451                 InsetMathNest::drawSelection(pi, x, y);
452 }
453
454
455 void MathMacro::setDisplayMode(MathMacro::DisplayMode mode, int appetite)
456 {
457         if (displayMode_ != mode) {             
458                 // transfer name if changing from or to DISPLAY_UNFOLDED
459                 if (mode == DISPLAY_UNFOLDED) {
460                         cells_.resize(1);
461                         asArray(name_, cell(0));
462                 } else if (displayMode_ == DISPLAY_UNFOLDED) {
463                         name_ = asString(cell(0));
464                         cells_.resize(0);
465                 }
466
467                 displayMode_ = mode;
468                 needsUpdate_ = true;
469         }
470         
471         // the interactive init mode is non-greedy by default
472         if (appetite == -1)
473                 appetite_ = (mode == DISPLAY_INTERACTIVE_INIT) ? 0 : 9;
474         else
475                 appetite_ = size_t(appetite);
476 }
477
478
479 MathMacro::DisplayMode MathMacro::computeDisplayMode() const
480 {
481         if (nextFoldMode_ == true && macro_ && !macro_->locked())
482                 return DISPLAY_NORMAL;
483         else
484                 return DISPLAY_UNFOLDED;
485 }
486
487
488 bool MathMacro::validName() const
489 {
490         docstring n = name();
491
492         // empty name?
493         if (n.size() == 0)
494                 return false;
495
496         // converting back and force doesn't swallow anything?
497         /*MathData ma;
498         asArray(n, ma);
499         if (asString(ma) != n)
500                 return false;*/
501
502         // valid characters?
503         for (size_t i = 0; i<n.size(); ++i) {
504                 if (!(n[i] >= 'a' && n[i] <= 'z')
505                     && !(n[i] >= 'A' && n[i] <= 'Z')
506                     && n[i] != '*') 
507                         return false;
508         }
509
510         return true;
511 }
512
513
514 void MathMacro::validate(LaTeXFeatures & features) const
515 {
516         if (!requires_.empty())
517                 features.require(requires_);
518
519         if (name() == "binom")
520                 features.require("binom");
521         
522         // validate the cells and the definition
523         if (displayMode() == DISPLAY_NORMAL) {
524                 definition_.validate(features);
525                 InsetMathNest::validate(features);
526         }
527 }
528
529
530 void MathMacro::edit(Cursor & cur, bool front, EntryDirection entry_from)
531 {
532         cur.updateFlags(Update::SinglePar);
533         InsetMathNest::edit(cur, front, entry_from);
534 }
535
536
537 Inset * MathMacro::editXY(Cursor & cur, int x, int y)
538 {
539         // We may have 0 arguments, but InsetMathNest requires at least one.
540         if (nargs() > 0) {
541                 cur.updateFlags(Update::SinglePar);
542                 return InsetMathNest::editXY(cur, x, y);                
543         } else
544                 return this;
545 }
546
547
548 void MathMacro::removeArgument(Inset::pos_type pos) {
549         if (displayMode_ == DISPLAY_NORMAL) {
550                 LASSERT(size_t(pos) < cells_.size(), /**/);
551                 cells_.erase(cells_.begin() + pos);
552                 if (size_t(pos) < attachedArgsNum_)
553                         --attachedArgsNum_;
554                 if (size_t(pos) < optionals_) {
555                         --optionals_;
556                 }
557
558                 needsUpdate_ = true;
559         }
560 }
561
562
563 void MathMacro::insertArgument(Inset::pos_type pos) {
564         if (displayMode_ == DISPLAY_NORMAL) {
565                 LASSERT(size_t(pos) <= cells_.size(), /**/);
566                 cells_.insert(cells_.begin() + pos, MathData());
567                 if (size_t(pos) < attachedArgsNum_)
568                         ++attachedArgsNum_;
569                 if (size_t(pos) < optionals_)
570                         ++optionals_;
571
572                 needsUpdate_ = true;
573         }
574 }
575
576
577 void MathMacro::detachArguments(vector<MathData> & args, bool strip)
578 {
579         LASSERT(displayMode_ == DISPLAY_NORMAL, /**/);  
580         args = cells_;
581
582         // strip off empty cells, but not more than arity-attachedArgsNum_
583         if (strip) {
584                 size_t i;
585                 for (i = cells_.size(); i > attachedArgsNum_; --i)
586                         if (!cell(i - 1).empty()) break;
587                 args.resize(i);
588         }
589
590         attachedArgsNum_ = 0;
591         expanded_.cell(0) = MathData();
592         cells_.resize(0);
593
594         needsUpdate_ = true;
595 }
596
597
598 void MathMacro::attachArguments(vector<MathData> const & args, size_t arity, int optionals)
599 {
600         LASSERT(displayMode_ == DISPLAY_NORMAL, /**/);
601         cells_ = args;
602         attachedArgsNum_ = args.size();
603         cells_.resize(arity);
604         expanded_.cell(0) = MathData();
605         optionals_ = optionals;
606
607         needsUpdate_ = true;
608 }
609
610
611 bool MathMacro::idxFirst(Cursor & cur) const 
612 {
613         cur.updateFlags(Update::SinglePar);
614         return InsetMathNest::idxFirst(cur);
615 }
616
617
618 bool MathMacro::idxLast(Cursor & cur) const 
619 {
620         cur.updateFlags(Update::SinglePar);
621         return InsetMathNest::idxLast(cur);
622 }
623
624
625 bool MathMacro::notifyCursorLeaves(Cursor const & old, Cursor & cur)
626 {
627         cur.updateFlags(Update::Force);
628         return InsetMathNest::notifyCursorLeaves(old, cur);
629 }
630
631
632 void MathMacro::fold(Cursor & cur)
633 {
634         if (!nextFoldMode_) {
635                 nextFoldMode_ = true;
636                 cur.updateFlags(Update::SinglePar);
637         }
638 }
639
640
641 void MathMacro::unfold(Cursor & cur)
642 {
643         if (nextFoldMode_) {
644                 nextFoldMode_ = false;
645                 cur.updateFlags(Update::SinglePar);
646         }
647 }
648
649
650 bool MathMacro::folded() const
651 {
652         return nextFoldMode_;
653 }
654
655
656 void MathMacro::write(WriteStream & os) const
657 {
658         MathEnsurer ensurer(os, macro_ != 0, true);
659
660         // non-normal mode
661         if (displayMode_ != DISPLAY_NORMAL) {
662                 os << "\\" << name();
663                 if (name().size() != 1 || isAlphaASCII(name()[0]))
664                         os.pendingSpace(true);
665                 return;
666         }
667
668         // normal mode
669         LASSERT(macro_, /**/);
670
671         // optional arguments make macros fragile
672         if (optionals_ > 0 && os.fragile())
673                 os << "\\protect";
674         
675         os << "\\" << name();
676         bool first = true;
677         
678         // Optional arguments:
679         // First find last non-empty optional argument
680         idx_type emptyOptFrom = 0;
681         idx_type i = 0;
682         for (; i < cells_.size() && i < optionals_; ++i) {
683                 if (!cell(i).empty())
684                         emptyOptFrom = i + 1;
685         }
686         
687         // print out optionals
688         for (i=0; i < cells_.size() && i < emptyOptFrom; ++i) {
689                 first = false;
690                 os << "[" << cell(i) << "]";
691         }
692         
693         // skip the tailing empty optionals
694         i = optionals_;
695         
696         // Print remaining macros 
697         for (; i < cells_.size(); ++i) {
698                 if (cell(i).size() == 1 
699                         && cell(i)[0].nucleus()->asCharInset()
700                         && cell(i)[0].nucleus()->asCharInset()->getChar() < 0x80) {
701                         if (first)
702                                 os << " ";
703                         os << cell(i);
704                 } else
705                         os << "{" << cell(i) << "}";
706                 first = false;
707         }
708
709         // add space if there was no argument
710         if (first)
711                 os.pendingSpace(true);
712 }
713
714
715 void MathMacro::maple(MapleStream & os) const
716 {
717         lyx::maple(expanded_.cell(0), os);
718 }
719
720
721 void MathMacro::mathmlize(MathStream & os) const
722 {
723         lyx::mathmlize(expanded_.cell(0), os);
724 }
725
726
727 void MathMacro::octave(OctaveStream & os) const
728 {
729         lyx::octave(expanded_.cell(0), os);
730 }
731
732
733 void MathMacro::infoize(odocstream & os) const
734 {
735         os << "Macro: " << name();
736 }
737
738
739 void MathMacro::infoize2(odocstream & os) const
740 {
741         os << "Macro: " << name();
742
743 }
744
745
746 bool MathMacro::completionSupported(Cursor const & cur) const
747 {
748         if (displayMode() != DISPLAY_UNFOLDED)
749                 return InsetMathNest::completionSupported(cur);
750
751         return lyxrc.completion_popup_math
752                 && displayMode() == DISPLAY_UNFOLDED
753                 && cur.bv().cursor().pos() == int(name().size());
754 }
755
756
757 bool MathMacro::inlineCompletionSupported(Cursor const & cur) const
758 {
759         if (displayMode() != DISPLAY_UNFOLDED)
760                 return InsetMathNest::inlineCompletionSupported(cur);
761
762         return lyxrc.completion_inline_math
763                 && displayMode() == DISPLAY_UNFOLDED
764                 && cur.bv().cursor().pos() == int(name().size());
765 }
766
767
768 bool MathMacro::automaticInlineCompletion() const
769 {
770         if (displayMode() != DISPLAY_UNFOLDED)
771                 return InsetMathNest::automaticInlineCompletion();
772
773         return lyxrc.completion_inline_math;
774 }
775
776
777 bool MathMacro::automaticPopupCompletion() const
778 {
779         if (displayMode() != DISPLAY_UNFOLDED)
780                 return InsetMathNest::automaticPopupCompletion();
781
782         return lyxrc.completion_popup_math;
783 }
784
785
786 CompletionList const * 
787 MathMacro::createCompletionList(Cursor const & cur) const
788 {
789         if (displayMode() != DISPLAY_UNFOLDED)
790                 return InsetMathNest::createCompletionList(cur);
791
792         return new MathCompletionList(cur.bv().cursor());
793 }
794
795
796 docstring MathMacro::completionPrefix(Cursor const & cur) const
797 {
798         if (displayMode() != DISPLAY_UNFOLDED)
799                 return InsetMathNest::completionPrefix(cur);
800
801         if (!completionSupported(cur))
802                 return docstring();
803         
804         return "\\" + name();
805 }
806
807
808 bool MathMacro::insertCompletion(Cursor & cur, docstring const & s,
809                                         bool finished)
810 {
811         if (displayMode() != DISPLAY_UNFOLDED)
812                 return InsetMathNest::insertCompletion(cur, s, finished);
813
814         if (!completionSupported(cur))
815                 return false;
816
817         // append completion
818         docstring newName = name() + s;
819         asArray(newName, cell(0));
820         cur.bv().cursor().pos() = name().size();
821         cur.updateFlags(Update::SinglePar);
822         
823         // finish macro
824         if (finished) {
825                 cur.bv().cursor().pop();
826                 ++cur.bv().cursor().pos();
827                 cur.updateFlags(Update::SinglePar);
828         }
829         
830         return true;
831 }
832
833
834 void MathMacro::completionPosAndDim(Cursor const & cur, int & x, int & y,
835         Dimension & dim) const
836 {
837         if (displayMode() != DISPLAY_UNFOLDED)
838                 InsetMathNest::completionPosAndDim(cur, x, y, dim);
839         
840         // get inset dimensions
841         dim = cur.bv().coordCache().insets().dim(this);
842         // FIXME: these 3 are no accurate, but should depend on the font.
843         // Now the popup jumps down if you enter a char with descent > 0.
844         dim.des += 3;
845         dim.asc += 3;
846         
847         // and position
848         Point xy
849         = cur.bv().coordCache().insets().xy(this);
850         x = xy.x_;
851         y = xy.y_;
852 }
853
854
855 } // namespace lyx