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