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