]> git.lyx.org Git - lyx.git/blob - src/mathed/MathMacro.cpp
* InsetMathHull:
[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/lassert.h"
36 #include "support/debug.h"
37
38 #include <ostream>
39 #include <vector>
40
41 using namespace std;
42
43 namespace lyx {
44
45
46 /// A proxy for the macro values
47 class ArgumentProxy : public InsetMath {
48 public:
49         ///
50         ArgumentProxy(MathMacro & mathMacro, size_t idx) 
51                 : mathMacro_(mathMacro), idx_(idx) {}
52         ///
53         ArgumentProxy(MathMacro & mathMacro, size_t idx, docstring const & def) 
54                 : mathMacro_(mathMacro), idx_(idx) 
55         {
56                         asArray(def, def_);
57         }
58         ///
59         void metrics(MetricsInfo & mi, Dimension & dim) const {
60                 mathMacro_.macro()->unlock();
61                 if (!mathMacro_.editMetrics(mi.base.bv) 
62                     && mathMacro_.cell(idx_).empty())
63                         def_.metrics(mi, dim);
64                 else {
65                         CoordCache & coords = mi.base.bv->coordCache();
66                         dim = coords.arrays().dim(&mathMacro_.cell(idx_));
67                 }
68                 mathMacro_.macro()->lock();
69         }
70         ///
71         void draw(PainterInfo & pi, int x, int y) const {
72                 if (mathMacro_.editMetrics(pi.base.bv)) {
73                         // The only way a ArgumentProxy can appear is in a cell of the 
74                         // MathMacro. Moreover the cells are only drawn in the DISPLAY_FOLDED 
75                         // mode and then, if the macro is edited the monochrome 
76                         // mode is entered by the MathMacro before calling the cells' draw
77                         // method. Then eventually this code is reached and the proxy leaves
78                         // monochrome mode temporarely. Hence, if it is not in monochrome 
79                         // here (and the assert triggers in pain.leaveMonochromeMode()) 
80                         // it's a bug.
81                         pi.pain.leaveMonochromeMode();
82                         mathMacro_.cell(idx_).draw(pi, x, y);
83                         pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
84                 } else if (mathMacro_.cell(idx_).empty()) {
85                         mathMacro_.cell(idx_).setXY(*pi.base.bv, x, y);
86                         def_.draw(pi, x, y);
87                 } else
88                         mathMacro_.cell(idx_).draw(pi, x, y);
89         }
90         ///
91         size_t idx() const { return idx_; }
92         ///
93         int kerning(BufferView const * bv) const
94         { 
95                 if (mathMacro_.editMetrics(bv)
96                     || !mathMacro_.cell(idx_).empty())
97                         return mathMacro_.cell(idx_).kerning(bv); 
98                 else
99                         return def_.kerning(bv);
100         }
101
102 private:
103         ///
104         Inset * clone() const 
105         {
106                 return new ArgumentProxy(*this);
107         }
108         ///
109         MathMacro & mathMacro_;
110         ///
111         size_t idx_;
112         ///
113         MathData def_;
114 };
115
116
117 MathMacro::MathMacro(docstring const & name)
118         : InsetMathNest(0), name_(name), displayMode_(DISPLAY_INIT),
119                 attachedArgsNum_(0), optionals_(0), nextFoldMode_(true),
120                 macro_(0), needsUpdate_(false), appetite_(9)
121 {}
122
123
124 Inset * MathMacro::clone() const
125 {
126         MathMacro * copy = new MathMacro(*this);
127         copy->needsUpdate_ = true;
128         copy->expanded_.cell(0).clear();
129         return copy;
130 }
131
132
133 docstring MathMacro::name() const
134 {
135         if (displayMode_ == DISPLAY_UNFOLDED)
136                 return asString(cell(0));
137         else
138                 return name_;
139 }
140
141
142 void MathMacro::cursorPos(BufferView const & bv,
143                 CursorSlice const & sl, bool boundary, int & x, int & y) const
144 {
145         // We may have 0 arguments, but InsetMathNest requires at least one.
146         if (nargs() > 0)
147                 InsetMathNest::cursorPos(bv, sl, boundary, x, y);
148 }
149
150
151 bool MathMacro::editMode(BufferView const * bv) const {
152         // find this in cursor trace
153         Cursor const & cur = bv->cursor();
154         for (size_t i = 0; i != cur.depth(); ++i)
155                 if (&cur[i].inset() == this) {
156                         // look if there is no other macro in edit mode above
157                         ++i;
158                         for (; i != cur.depth(); ++i) {
159                                 MathMacro const * macro = dynamic_cast<MathMacro const *>(&cur[i].inset());
160                                 if (macro && macro->displayMode() == DISPLAY_NORMAL)
161                                         return false;
162                         }
163
164                         // ok, none found, I am the highest one
165                         return true;
166                 }
167
168         return false;
169 }
170
171
172 bool MathMacro::editMetrics(BufferView const * bv) const
173 {
174         return editing_[bv];
175 }
176
177
178 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
179 {
180         // set edit mode for which we will have calculated metrics. But only
181         editing_[mi.base.bv] = editMode(mi.base.bv);
182
183         // calculate new metrics according to display mode
184         if (displayMode_ == DISPLAY_INIT || displayMode_ == DISPLAY_INTERACTIVE_INIT) {
185                 mathed_string_dim(mi.base.font, from_ascii("\\") + name(), dim);
186         } else if (displayMode_ == DISPLAY_UNFOLDED) {
187                 cell(0).metrics(mi, dim);
188                 Dimension bsdim;
189                 mathed_string_dim(mi.base.font, from_ascii("\\"), bsdim);
190                 dim.wid += bsdim.width() + 1;
191                 dim.asc = max(bsdim.ascent(), dim.ascent());
192                 dim.des = max(bsdim.descent(), dim.descent());
193                 metricsMarkers(dim);
194         } else if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_LIST 
195                    && editing_[mi.base.bv]) {
196                 // Macro will be edited in a old-style list mode here:
197
198                 LASSERT(macro_ != 0, /**/);
199                 Dimension fontDim;
200                 FontInfo labelFont = sane_font;
201                 math_font_max_dim(labelFont, fontDim.asc, fontDim.des);
202                 
203                 // get dimension of components of list view
204                 Dimension nameDim;
205                 nameDim.wid = mathed_string_width(mi.base.font, from_ascii("Macro \\") + name() + ": ");
206                 nameDim.asc = fontDim.asc;
207                 nameDim.des = fontDim.des;
208
209                 Dimension argDim;
210                 argDim.wid = mathed_string_width(labelFont, from_ascii("#9: "));
211                 argDim.asc = fontDim.asc;
212                 argDim.des = fontDim.des;
213                 
214                 Dimension defDim;
215                 definition_.metrics(mi, defDim);
216                 
217                 // add them up
218                 dim.wid = nameDim.wid + defDim.wid;
219                 dim.asc = max(nameDim.asc, defDim.asc);
220                 dim.des = max(nameDim.des, defDim.des);
221                 
222                 for (idx_type i = 0; i < nargs(); ++i) {
223                         Dimension cdim;
224                         cell(i).metrics(mi, cdim);
225                         dim.des += max(argDim.height(), cdim.height()) + 1;
226                         dim.wid = max(dim.wid, argDim.wid + cdim.wid);
227                 }
228                 
229                 // make space for box and markers, 2 pixels
230                 dim.asc += 1;
231                 dim.des += 1;
232                 dim.wid += 2;
233                 metricsMarkers2(dim);
234         } else {
235                 LASSERT(macro_ != 0, /**/);
236
237                 // metrics are computed here for the cells,
238                 // in the proxy we will then use the dim from the cache
239                 InsetMathNest::metrics(mi);
240                 
241                 // calculate metrics finally
242                 macro_->lock();
243                 expanded_.cell(0).metrics(mi, dim);
244                 macro_->unlock();
245
246                 // calculate dimension with label while editing
247                 if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_INLINE_BOX 
248                     && editing_[mi.base.bv]) {
249                         FontInfo font = mi.base.font;
250                         augmentFont(font, from_ascii("lyxtex"));
251                         Dimension namedim;
252                         mathed_string_dim(font, name(), namedim);
253 #if 0
254                         dim.wid += 2 + namedim.wid + 2 + 2;
255                         dim.asc = max(dim.asc, namedim.asc) + 2;
256                         dim.des = max(dim.des, namedim.des) + 2;
257 #endif
258                         dim.wid = max(1 + namedim.wid + 1, 2 + dim.wid + 2);
259                         dim.asc += 1 + namedim.height() + 1;
260                         dim.des += 2;
261                 }
262          
263         }
264 }
265
266
267 int MathMacro::kerning(BufferView const * bv) const {
268         if (displayMode_ == DISPLAY_NORMAL && !editing_[bv])
269                 return expanded_.kerning(bv);
270         else
271                 return 0;
272 }
273
274
275 void MathMacro::updateMacro(MacroContext const & mc) 
276 {
277         if (validName()) {
278                 macro_ = mc.get(name());            
279                 if (macro_ && macroBackup_ != *macro_) {
280                         macroBackup_ = *macro_;
281                         needsUpdate_ = true;
282                 }
283         } else {
284                 macro_ = 0;
285         }
286 }
287
288
289 void MathMacro::updateRepresentation()
290 {
291         // known macro?
292         if (macro_ == 0)
293                 return;
294
295         // update requires
296         requires_ = macro_->requires();
297         
298         // non-normal mode? We are done!
299         if (displayMode_ != DISPLAY_NORMAL)
300                 return;
301
302         // macro changed?
303         if (!needsUpdate_)
304                 return;
305         
306         needsUpdate_ = false;
307         
308         // get default values of macro
309         vector<docstring> const & defaults = macro_->defaults();
310         
311         // create MathMacroArgumentValue objects pointing to the cells of the macro
312         vector<MathData> values(nargs());
313         for (size_t i = 0; i < nargs(); ++i) {
314                 ArgumentProxy * proxy;
315                 if (i < defaults.size()) 
316                         proxy = new ArgumentProxy(*this, i, defaults[i]);
317                 else
318                         proxy = new ArgumentProxy(*this, i);
319                 values[i].insert(0, MathAtom(proxy));
320         }
321         
322         // expanding macro with the values
323         macro_->expand(values, expanded_.cell(0));
324                 // get definition for list edit mode
325         docstring const & display = macro_->display();
326         asArray(display.empty() ? macro_->definition() : display, definition_);
327 }
328
329
330 void MathMacro::draw(PainterInfo & pi, int x, int y) const
331 {
332         Dimension const dim = dimension(*pi.base.bv);
333
334         setPosCache(pi, x, y);
335         int expx = x;
336         int expy = y;
337
338         if (displayMode_ == DISPLAY_INIT || displayMode_ == DISPLAY_INTERACTIVE_INIT) {         
339                 PainterInfo pi2(pi.base.bv, pi.pain);
340                 pi2.base.font.setColor(macro_ ? Color_latex : Color_error);
341                 //pi2.base.style = LM_ST_TEXT;
342                 pi2.pain.text(x, y, from_ascii("\\") + name(), pi2.base.font);
343         } else if (displayMode_ == DISPLAY_UNFOLDED) {
344                 PainterInfo pi2(pi.base.bv, pi.pain);
345                 pi2.base.font.setColor(macro_ ? Color_latex : Color_error);
346                 //pi2.base.style = LM_ST_TEXT;
347                 pi2.pain.text(x, y, from_ascii("\\"), pi2.base.font);
348                 x += mathed_string_width(pi2.base.font, from_ascii("\\")) + 1;
349                 cell(0).draw(pi2, x, y);
350                 drawMarkers(pi2, expx, expy);
351         } else if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_LIST
352                    && editing_[pi.base.bv]) {
353                 // Macro will be edited in a old-style list mode here:
354                 
355                 CoordCache & coords = pi.base.bv->coordCache();
356                 FontInfo const & labelFont = sane_font;
357                 
358                 // markers and box needs two pixels
359                 x += 2;
360                 
361                 // get maximal font height
362                 Dimension fontDim;
363                 math_font_max_dim(pi.base.font, fontDim.asc, fontDim.des);
364                 
365                 // draw label
366                 docstring label = from_ascii("Macro \\") + name() + from_ascii(": ");
367                 pi.pain.text(x, y, label, labelFont);
368                 x += mathed_string_width(labelFont, label);
369
370                 // draw definition
371                 definition_.draw(pi, x, y);
372                 Dimension defDim
373                 = coords.arrays().dim(&definition_);
374                 y += max(fontDim.des, defDim.des);
375                                 
376                 // draw parameters
377                 docstring str = from_ascii("#9");
378                 int strw1 = mathed_string_width(labelFont, from_ascii("#9"));
379                 int strw2 = mathed_string_width(labelFont, from_ascii(": "));
380                 
381                 for (idx_type i = 0; i < nargs(); ++i) {
382                         // position of label
383                         Dimension cdim
384                         = coords.arrays().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" || name() == "mathcircumflex")
520                 features.require(to_utf8(name()));
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         // non-normal mode
659         if (displayMode_ != DISPLAY_NORMAL) {
660                 os << "\\" << name() << " ";
661                 os.pendingSpace(true);
662                 return;
663         }
664
665         // normal mode
666         LASSERT(macro_, /**/);
667
668         // optional arguments make macros fragile
669         if (optionals_ > 0 && os.fragile())
670                 os << "\\protect";
671         
672         os << "\\" << name();
673         bool first = true;
674         
675         // Optional arguments:
676         // First find last non-empty optional argument
677         idx_type emptyOptFrom = 0;
678         idx_type i = 0;
679         for (; i < cells_.size() && i < optionals_; ++i) {
680                 if (!cell(i).empty())
681                         emptyOptFrom = i + 1;
682         }
683         
684         // print out optionals
685         for (i=0; i < cells_.size() && i < emptyOptFrom; ++i) {
686                 first = false;
687                 os << "[" << cell(i) << "]";
688         }
689         
690         // skip the tailing empty optionals
691         i = optionals_;
692         
693         // Print remaining macros 
694         for (; i < cells_.size(); ++i) {
695                 if (cell(i).size() == 1 
696                          && cell(i)[0].nucleus()->asCharInset()) {
697                         if (first)
698                                 os << " ";
699                         os << cell(i);
700                 } else
701                         os << "{" << cell(i) << "}";
702                 first = false;
703         }
704
705         // add space if there was no argument
706         if (first)
707                 os.pendingSpace(true);
708 }
709
710
711 void MathMacro::maple(MapleStream & os) const
712 {
713         lyx::maple(expanded_.cell(0), os);
714 }
715
716
717 void MathMacro::mathmlize(MathStream & os) const
718 {
719         lyx::mathmlize(expanded_.cell(0), os);
720 }
721
722
723 void MathMacro::octave(OctaveStream & os) const
724 {
725         lyx::octave(expanded_.cell(0), os);
726 }
727
728
729 void MathMacro::infoize(odocstream & os) const
730 {
731         os << "Macro: " << name();
732 }
733
734
735 void MathMacro::infoize2(odocstream & os) const
736 {
737         os << "Macro: " << name();
738
739 }
740
741
742 bool MathMacro::completionSupported(Cursor const & cur) const
743 {
744         if (displayMode() != DISPLAY_UNFOLDED)
745                 return InsetMathNest::completionSupported(cur);
746
747         return lyxrc.completion_popup_math
748                 && displayMode() == DISPLAY_UNFOLDED
749                 && cur.bv().cursor().pos() == int(name().size());
750 }
751
752
753 bool MathMacro::inlineCompletionSupported(Cursor const & cur) const
754 {
755         if (displayMode() != DISPLAY_UNFOLDED)
756                 return InsetMathNest::inlineCompletionSupported(cur);
757
758         return lyxrc.completion_inline_math
759                 && displayMode() == DISPLAY_UNFOLDED
760                 && cur.bv().cursor().pos() == int(name().size());
761 }
762
763
764 bool MathMacro::automaticInlineCompletion() const
765 {
766         if (displayMode() != DISPLAY_UNFOLDED)
767                 return InsetMathNest::automaticInlineCompletion();
768
769         return lyxrc.completion_inline_math;
770 }
771
772
773 bool MathMacro::automaticPopupCompletion() const
774 {
775         if (displayMode() != DISPLAY_UNFOLDED)
776                 return InsetMathNest::automaticPopupCompletion();
777
778         return lyxrc.completion_popup_math;
779 }
780
781
782 CompletionList const * 
783 MathMacro::createCompletionList(Cursor const & cur) const
784 {
785         if (displayMode() != DISPLAY_UNFOLDED)
786                 return InsetMathNest::createCompletionList(cur);
787
788         return new MathCompletionList(cur.bv().cursor());
789 }
790
791
792 docstring MathMacro::completionPrefix(Cursor const & cur) const
793 {
794         if (displayMode() != DISPLAY_UNFOLDED)
795                 return InsetMathNest::completionPrefix(cur);
796
797         if (!completionSupported(cur))
798                 return docstring();
799         
800         return "\\" + name();
801 }
802
803
804 bool MathMacro::insertCompletion(Cursor & cur, docstring const & s,
805                                         bool finished)
806 {
807         if (displayMode() != DISPLAY_UNFOLDED)
808                 return InsetMathNest::insertCompletion(cur, s, finished);
809
810         if (!completionSupported(cur))
811                 return false;
812
813         // append completion
814         docstring newName = name() + s;
815         asArray(newName, cell(0));
816         cur.bv().cursor().pos() = name().size();
817         cur.updateFlags(Update::SinglePar);
818         
819         // finish macro
820         if (finished) {
821                 cur.bv().cursor().pop();
822                 ++cur.bv().cursor().pos();
823                 cur.updateFlags(Update::SinglePar);
824         }
825         
826         return true;
827 }
828
829
830 void MathMacro::completionPosAndDim(Cursor const & cur, int & x, int & y,
831         Dimension & dim) const
832 {
833         if (displayMode() != DISPLAY_UNFOLDED)
834                 InsetMathNest::completionPosAndDim(cur, x, y, dim);
835         
836         // get inset dimensions
837         dim = cur.bv().coordCache().insets().dim(this);
838         // FIXME: these 3 are no accurate, but should depend on the font.
839         // Now the popup jumps down if you enter a char with descent > 0.
840         dim.des += 3;
841         dim.asc += 3;
842         
843         // and position
844         Point xy
845         = cur.bv().coordCache().insets().xy(this);
846         x = xy.x_;
847         y = xy.y_;
848 }
849
850
851 } // namespace lyx